GAZAR

Principal Engineer | Mentor

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

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

A Node.js buffer is a contiguous block of memory that stores binary data. It is a low-level, binary data type that is used to store and manipulate data in Node.js. Buffers are often used to read and write files, send and receive data over a network, and process data in memory.

What is a Node.js Stream?

A Node.js stream is a sequence of data that is processed in a continuous flow. Streams are designed to handle large amounts of data by breaking it down into smaller, manageable chunks. Streams are often used to process data in real-time, such as processing video or audio files, or handling large amounts of network traffic.

import * as fs from 'fs';
import * as stream from 'stream';

const buffer = Buffer.from('Hello, World!', 'utf8');
fs.writeFileSync('example.txt', buffer.toString());
// OR
const buffer = fs.readFileSync('example.txt', 'utf8');
console.log(buffer.toString());
// OR
const buffer = Buffer.from('Hello, World!', 'utf8');
const socket = new net.Socket();
socket.connect(8080, 'localhost', () => {
  socket.write(buffer);
  socket.end();
});
// OR
const buffer = Buffer.from('Hello, World!', 'utf8');
const encrypted = crypto.createCipher('aes-256-cbc', 'mysecretkey');
const encryptedBuffer = Buffer.concat([encrypted.update(buffer), encrypted.final()]);
console.log(encryptedBuffer.toString());


const stream = new stream.Readable({
  read(size: number): void {
    const chunk = buffer.slice(0, size);
    this.push(chunk);
  }
});

stream.on('data', (chunk) => {
  console.log(chunk.toString());
});

stream.pipe(process.stdout);

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

Buffers are often used for file I/O, network I/O, and cryptography, whereas streams are used for real-time data processing, such as video or audio processing.

In conclusion, Node.js streams and buffers are two fundamental concepts in Node.js that serve different purposes. While buffers are used for low-level, binary data processing, streams are used for real-time data processing. Understanding the differences between these two concepts is crucial for building efficient and scalable Node.js applications.