Mid-levelMid (3–6 yrs)CodingReactAmazonNetflix
Node.js Streams Basics
When should you use Node.js streams instead of buffering entire files in memory?
Answers use simple, clear English.
Quick interview answer
Streams process data chunk-by-chunk for large payloads — uploads, downloads, transforms — keeping memory bounded. Prefer pipeline() for error propagation. Buffering a multi-GB file into a string will OOM.
Detailed answer
Streams process data chunk-by-chunk for large payloads — uploads, downloads, transforms — keeping memory bounded. Prefer pipeline() for error propagation. Buffering a multi-GB file into a string will OOM.
Real example & use case
Compressing multi-GB logs to S3 without loading the whole file into RAM.
Pros & cons
Pros: bounded memory. Cons: backpressure and error handling are harder than sync code.
Code example
import { createReadStream, createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';
import { createGzip } from 'zlib';
await pipeline(
createReadStream('big.log'),
createGzip(),
createWriteStream('big.log.gz'),
);Practice code · javascript (view only · no execution)
import { createReadStream, createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';
import { createGzip } from 'zlib';
await pipeline(
createReadStream('big.log'),
createGzip(),
createWriteStream('big.log.gz'),
);#nodejs#streams