JavaScript Streams: Readable, Writable, Duplex, Transform

Streams in JavaScript are fundamental objects used to handle reading and writing data sequentially chunk by chunk, preventing high memory usage when processing large datasets. This guide explains the core differences between the four primary stream types in JavaScript—Readable, Writable, Duplex, and Transform streams—covering their mechanisms, standard implementations, and specific use cases.

1. Readable Streams

A Readable stream represents a source from which data can be consumed. It produces data sequentially and passes it into a buffer until a consumer reads it.

2. Writable Streams

A Writable stream represents a destination where data can be written. It consumes chunks of data and writes them to a specific target, handling backpressure automatically when the destination cannot keep up with the incoming rate.

3. Duplex Streams

A Duplex stream is a stream that implements both the Readable and Writable interfaces. It can both produce and consume data independently. The read and write channels operate separately, meaning the data being read does not necessarily depend on or modify the data being written.

4. Transform Streams

A Transform stream is a specialized type of Duplex stream where the output is causally linked to the input. Instead of maintaining independent read and write channels, a Transform stream accepts input data, applies a transformation algorithm to it, and outputs the modified data to be read.

Key Differences Summary

Stream Type Can Read? Can Write? Input/Output Relationship Primary Purpose
Readable Yes No None (Source only) Fetching data from a source
Writable No Yes None (Destination only) Sending data to a destination
Duplex Yes Yes Independent channels Two-way communication
Transform Yes Yes Dependent (Output derived from input) Data modification and processing