Skip to content

TypeScript

What is the difference between a Node.js stream and a Node.js buffer?

Think of a buffer as a bucket. You fill it up, then you use the water. Think of a stream as a pipe. Water flows through it continuously.

23 Apr 2024 · 2 min read

What is the difference between a Node.js stream and a Node.js buffer?

Think of a buffer as a bucket. You fill it up, then you use the water. Think of a stream as a pipe. Water flows through it continuously.

That's the core difference in Node.js.

Buffer — load everything into memory

A buffer is a chunk of binary data sitting in memory. You read the whole thing at once, work with it, then move on.

Typescript
import * as fs from 'fs';

const buffer = Buffer.from('Hello, World!', 'utf8');
fs.writeFileSync('example.txt', buffer.toString());

const fileContent = fs.readFileSync('example.txt', 'utf8');
console.log(fileContent);

Buffers work great for small files and quick operations. But load a 2GB video file into a buffer and your process crashes with an out-of-memory error.

Stream — process data in chunks

A stream processes data piece by piece as it arrives. You never hold the entire dataset in memory.

Typescript
import * as fs from 'fs';

const stream = fs.createReadStream('largefile.txt', 'utf8');
stream.on('data', (chunk) => {
  console.log(chunk.toString());
});

stream.pipe(process.stdout);

Streams shine for large files, network I/O, and real-time processing like video or audio.

When to use which

Buffers: Small data, file reads where you need the entire content at once, encryption operations, quick transformations.

Streams: Large files, real-time data, piping data between sources, anything where memory efficiency matters.

The trade-off

Buffers are simpler to reason about — you have all the data, do what you want. Streams are more complex (backpressure, error handling across chunks) but they scale. If your data fits comfortably in memory, a buffer is fine. If it doesn't, you need a stream.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading