Course contentsShow
← JavaScript
Lesson 1548 of 2,72334. Streams and BuffersFree lesson

What Are Streams?

Understanding streams as sequences of data processed piece-by-piece rather than all at once.

What Are Streams?

You'll learn: how streams allow Node.js to handle data piece-by-piece instead of loading everything into memory at once.

The Core Idea

Imagine you need to transfer water from one container to another. You could wait until you've collected all the water, then move it in one giant batch. Or you could use a hose and let the water flow continuously in smaller amounts.

Streams are the hose approach for data.

In Node.js, a stream is a sequence of data that's processed incrementally—chunk by chunk—rather than loading the entire dataset into memory at once. This is especially powerful when working with large files, network requests, or any data source where waiting for everything to arrive would be slow or memory-intensive.

Why Streams Matter

When you use fs.readFile(), Node.js loads the entire file into memory before your callback runs. For a 2GB video file, that's 2GB of RAM consumed immediately. With streams, you can read that same file in small chunks (like 64KB at a time), process each piece, and move on—never holding the whole thing in memory.

Streams aren't just for files—they work with HTTP requests, database queries, compression, and more. They're fundamental to how Node.js handles I/O efficiently.

The Four Stream Types (Preview)

Node.js has four basic stream types: Readable (you consume data), Writable (you send data), Duplex (both), and Transform (modify data as it flows). You'll explore these in upcoming lessons.

Key Takeaway: Streams process data piece-by-piece as it flows, enabling efficient handling of large datasets without overwhelming memory—think continuous flow rather than batch loading.