Search lands in PR-5.1 (Pagefind).

Explanation Intermediate

Chapter 11 Updated

Streams & Buffers

Why Node moves bytes in streams, and what backpressure really is.

  • Full 35m
  • Revision 4m
  • Flow 2m

Buffers

A Buffer is a fixed-size chunk of raw bytes outside V8’s heap. Strings are UTF-16 in JavaScript; buffers are whatever encoding you choose. Most Node APIs hand you buffers because that’s what the OS handed Node.

const b = Buffer.from('hello', 'utf8');
b.length;       // 5
b.toString();   // 'hello'

Streams

A stream is an async iterator over chunks. The four flavors: Readable, Writable, Duplex, Transform. You read from a Readable, write to a Writable, and pipe connects them.

import { createReadStream, createWriteStream } from 'node:fs';
createReadStream('in.log').pipe(createWriteStream('out.log'));

Backpressure

If the consumer is slow, a naive loop fills memory. Streams solve this: write() returns false when the internal buffer is full, and you pause the producer until 'drain'. pipe handles this for you.

Placeholder chapter — full worked examples and benchmarks coming soon.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.