0
0
Node.jsframework~8 mins

Writing data with Writable streams in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Writing data with Writable streams
MEDIUM IMPACT
This concept affects how efficiently data is written to streams, impacting memory usage and responsiveness during I/O operations.
Writing large amounts of data to a Writable stream without handling backpressure
Node.js
const fs = require('fs');
const writable = fs.createWriteStream('output.txt');
function writeData(i) {
  let ok = true;
  while (i < 1e6 && ok) {
    ok = writable.write('Line ' + i + '\n');
    i++;
  }
  if (i < 1e6) {
    writable.once('drain', () => writeData(i));
  } else {
    writable.end();
  }
}
writeData(0);
Checks the return value of write() and waits for 'drain' event to avoid overwhelming the buffer, reducing memory spikes and event loop blocking.
📈 Performance GainPrevents buffer overflow, reduces memory usage spikes, and keeps event loop responsive
Writing large amounts of data to a Writable stream without handling backpressure
Node.js
const fs = require('fs');
const writable = fs.createWriteStream('output.txt');
for (let i = 0; i < 1e6; i++) {
  writable.write('Line ' + i + '\n');
}
writable.end();
Writing data continuously without checking if the stream buffer is full causes high memory usage and can block the event loop.
📉 Performance CostTriggers multiple internal buffer fills causing high memory pressure and potential event loop delays
Performance Comparison
PatternMemory UsageEvent Loop BlockingBackpressure HandlingVerdict
Writing without backpressure checkHigh (buffers grow large)Yes (blocks event loop)No[X] Bad
Writing with backpressure and drain eventLow (buffers controlled)No (event loop stays responsive)Yes[OK] Good
Rendering Pipeline
Writing data to streams flows through the Node.js event loop and internal buffer management before reaching the OS for actual I/O. Proper backpressure handling ensures smooth data flow without blocking.
Buffering
Event Loop
I/O System Calls
⚠️ BottleneckBuffering stage when internal buffer is full causing write() to return false and blocking further writes
Core Web Vital Affected
INP
This concept affects how efficiently data is written to streams, impacting memory usage and responsiveness during I/O operations.
Optimization Tips
1Always check the return value of writable.write() to detect backpressure.
2Pause writing when write() returns false and wait for the 'drain' event before resuming.
3Avoid writing large amounts of data synchronously to prevent blocking the event loop.
Performance Quiz - 3 Questions
Test your performance knowledge
What happens if you write data to a Writable stream without checking the write() return value?
AThe data is written instantly without any delay or memory impact.
BThe internal buffer may overflow causing high memory use and event loop blocking.
CThe stream automatically throttles without developer intervention.
DThe stream throws an error immediately.
DevTools: Node.js --inspect with Chrome DevTools Performance panel
How to check: Run your Node.js script with --inspect flag, open Chrome DevTools, record a performance profile during stream writing, and observe event loop delays and memory usage.
What to look for: Look for long event loop blocking times and high memory usage spikes indicating poor backpressure handling.