Readable and Writable Streams
Streams process data incrementally instead of requiring the entire dataset to be loaded into memory. Readable streams produce data, writable streams consume data, and duplex or transform streams can do both.
Streams are particularly valuable for large files, HTTP requests, HTTP responses, compression, and network communication.
Streaming a File
import { createReadStream } from 'node:fs';
const stream = createReadStream('large-file.txt', {
encoding: 'utf8'
});
stream.on('data', (chunk) => {
console.log('Received chunk:', chunk.length);
});
stream.on('end', () => {
console.log('Finished');
});💡 The file is delivered in chunks rather than loaded completely into memory.
Key Takeaways
- Streams reduce memory usage for large datasets.
- Readable streams produce chunks of data.
- Writable streams consume chunks of data.
Knowledge Check
What is a major advantage of streams?