NODEJS Contents

Streams Intro: Why Streams Exist

Use streams to process large data with constant memory, predictable latency, and safer error propagation in production pipelines.

On this page

Why Streams Matter in Production

Streams allow processing data incrementally instead of loading it entirely into memory. They are fundamental for scalable systems handling logs, uploads, or external API data.

Readable, Writable, Transform

Readable streams emit data chunks, writable streams consume them, and transform streams modify data in transit. Understanding their lifecycle is essential for building efficient pipelines.

import { createReadStream } from 'fs';

const stream = createReadStream('./large.log');
stream.on('data', chunk => {
  console.log(chunk.length);
});

Pipeline and Error Handling

In production, always use pipeline() to propagate errors correctly across chained streams. Silent stream failures are difficult to debug and may cause data loss.

Performance Insight

Streams reduce GC pressure and memory fragmentation under heavy workloads. They are preferred in high-throughput APIs and ETL systems.