Performance: Streams vs loading entire file in memory
HIGH IMPACT
This concept affects how fast a Node.js app can start processing data and how much memory it uses during file handling.
import fs from 'fs'; const stream = fs.createReadStream('largefile.txt', { encoding: 'utf-8' }); stream.on('data', chunk => console.log(chunk));
import fs from 'fs'; const data = fs.readFileSync('largefile.txt', 'utf-8'); console.log(data);
| Pattern | Memory Usage | Event Loop Blocking | Start Processing Time | Verdict |
|---|---|---|---|---|
| Load entire file with readFileSync | High (proportional to file size) | Yes (blocks event loop) | Delayed until full file loaded | [X] Bad |
| Read file with createReadStream | Low (small chunks buffered) | No (non-blocking) | Immediate with first chunk | [OK] Good |