Performance: Why streams are needed
HIGH IMPACT
Streams affect how efficiently data is processed and transferred, impacting memory usage and responsiveness during I/O operations.
const fs = require('fs'); const stream = fs.createReadStream('largefile.txt'); stream.on('data', chunk => sendDataOverNetwork(chunk));
const fs = require('fs'); const data = fs.readFileSync('largefile.txt'); sendDataOverNetwork(data);
| Pattern | Memory Usage | Event Loop Blocking | Responsiveness | Verdict |
|---|---|---|---|---|
| Read entire file at once | High (proportional to file size) | Yes (blocks event loop) | Poor (delays other tasks) | [X] Bad |
| Read file using streams | Low (fixed small chunks) | No (non-blocking) | Good (keeps app responsive) | [OK] Good |