Performance: Stream backpressure concept
HIGH IMPACT
This concept affects how fast data flows through streams, impacting memory usage and responsiveness during data processing.
const fs = require('fs'); const readable = fs.createReadStream('largefile.txt'); const writable = fs.createWriteStream('output.txt'); readable.on('data', chunk => { if (!writable.write(chunk)) { readable.pause(); } }); writable.on('drain', () => { readable.resume(); });
const fs = require('fs'); const readable = fs.createReadStream('largefile.txt'); const writable = fs.createWriteStream('output.txt'); readable.on('data', chunk => { writable.write(chunk); });
| Pattern | Memory Usage | Event Loop Blocking | Data Flow Control | Verdict |
|---|---|---|---|---|
| No backpressure handling | High and uncontrolled | Likely blocking | None, data floods buffers | [X] Bad |
| Proper backpressure handling | Controlled and low | Non-blocking | Pauses and resumes flow as needed | [OK] Good |