🔍 Introduction

In Node.js, handling large files or continuous data (such as video, audio, or network requests) can be slow and memory-intensive when loading everything at once. Streams and buffers help by allowing data to be processed in small parts instead of all together. This approach is faster and uses less memory.

📦 What is a Buffer?

A buffer is like a temporary box in memory where binary data is stored while moving between different places in your application.

In simple words:

Example:

// Creating and using a buffer
const buffer = Buffer.from('Hello Node.js');
console.log(buffer); 
console.log(buffer.toString()); 

Buffer.from creates binary data from a string, and toString() turns it back into readable text.

🌊 What is a Stream?

A stream is like a pipeline that lets you read or write data in a continuous flow without waiting for the whole data to be ready.

Types of Streams in Node.js:

Example:

// Reading a file with a stream
const fs = require('fs');

const readableStream = fs.createReadStream('largefile.txt', { encoding: 'utf8' });

readableStream.on('data', (chunk) => {
  console.log('Received chunk:', chunk);
});

readableStream.on('end', () => {
  console.log('Finished reading file');
});

Instead of loading the entire file at once, the file is read in smaller parts (chunks) and processed immediately.

⚡ How Streams and Buffers Work Together

Example:

// Copying a file using streams
const fs = require('fs');

const readable = fs.createReadStream('video.mp4');
const writable = fs.createWriteStream('copy.mp4');

readable.pipe(writable);

The pipe() method connects the readable stream (source) to the writable stream (destination), and Node.js handles buffering automatically.

📊 Benefits of Using Streams and Buffers

📝 Summary

In Node.js, buffers store binary data temporarily, and streams process that data in small chunks without waiting for all of it to arrive. This makes applications faster, more memory-efficient, and scalable. Together, they are essential for handling big files, live video/audio, or network communication efficiently.