Performance: Transform streams for processing
MEDIUM IMPACT
This affects how efficiently data is processed and passed through the pipeline without blocking the event loop or causing memory bloat.
const { Transform } = require('stream');
const fs = require('fs');
const upperCaseTransform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
fs.createReadStream('largefile.txt')
.pipe(upperCaseTransform)
.pipe(fs.createWriteStream('output.txt'));const fs = require('fs'); const data = fs.readFileSync('largefile.txt', 'utf8'); const processed = data.toUpperCase(); fs.writeFileSync('output.txt', processed);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous full file read/write | N/A | N/A | N/A | [X] Bad |
| Asynchronous transform streams chunk processing | N/A | N/A | N/A | [OK] Good |