Skip to content

Beginning of the lesson

Streams in Depth

Process data piece by piece instead of loading it all into memory

Big idea: A stream is an abstraction for data that arrives (or leaves) over time in small chunks, rather than as one finished blob. Streams let a Node process move gigabytes of data through only a few kilobytes of RAM, and they are the mechanism behind files, HTTP requests and responses, sockets, compression, and much more.
By the end you will be able to:
  • tell the four stream types apart -- Readable, Writable, Duplex, Transform;
  • consume a Readable in flowing mode, paused mode, and with for await;
  • connect streams with pipe() and pipeline();
  • explain backpressure and why it protects memory;
  • write your own Transform to reshape data as it flows.

1. Why streams exist

Imagine you must copy a 4 GB video file. The naive approach reads the whole file into a variable, then writes that variable out:

const data = fs.readFileSync('movie.mp4'); // 4 GB now lives in RAM
fs.writeFileSync('copy.mp4', data);

This works for small files but is a disaster for large ones: your process needs 4 GB of memory just to hold the buffer, and nothing happens until the entire read finishes. Streams solve both problems by moving the data in chunks (typically 64 KB at a time). Memory stays tiny and constant, and work starts on the first chunk immediately.

Analogy. A non-stream approach is like emptying a swimming pool by carrying the whole pool to a new location. A stream is a hose: water flows through continuously, you never hold more than what is in the hose at any instant, and the far end starts filling right away.

Because streams are an interface (an agreed set of methods and events), the same code that reads a file can read from a network socket or a decompressor. That composability is their real power.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers