WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Intermediate · question 21 of 100

What is the difference between ’Buffer’ and ’Stream’ in Node.js?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

In Node.js, ‘Buffer‘ and ‘Stream‘ are two important concepts for handling binary data efficiently. They have different use cases and focus on different aspects of data handling. Here’s a detailed comparison between them:

1. **Buffer**

A ‘Buffer‘ is a chunk of memory used to handle binary data, like files, images, or network requests. ‘Buffer‘ is a low-level data structure specifically designed to store and manipulate raw bytes. Node.js introduced ‘Buffer‘ because JavaScript was initially created for working with textual content and not for binary data manipulation.

Example: Reading a file into a buffer:

const fs = require('fs');

fs.readFile('example.txt', (err, data) => {
  if (err) throw err;
  console.log(data);
});

In this example, ‘data‘ is of the ‘Buffer‘ type.

Key aspects of ‘Buffer‘:

- Fixed-size, resizable only by creating a new buffer.

- Binary data abstraction (e.g., for character encoding handling or low-level operations).

- Efficient memory allocation and management.

2. **Stream**

A ‘Stream‘ is a way to handle continuous data flows, which can be processed piece by piece. It enables you to work with potentially large amounts of data without having the entire data in memory at once. Streams are particularly useful when dealing with large files, as you don’t need to read the entire file into memory to work with it. Instead, you can process the data in chunks, making it suitable for real-time data processing.

Example: Reading a file using streams:

const fs = require('fs');
const readStream = fs.createReadStream('example.txt');

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

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

In this example, the ‘chunk‘ is of the ‘Buffer‘ type.

Key aspects of ‘Stream‘:

- Asynchronous data handling and processing.

- Supports backpressure (controlling the flow of data to prevent data loss or overloading).

- Efficient handling of large data sets.

- Supports four types of streams: _Readable_, _Writable_, _Duplex_, and _Transform_.

In summary, while ‘Buffer‘ is focused on dealing with raw binary data in memory, ‘Stream‘ is focused on processing data flows in a more granular and efficient way. These tools are used together to deliver optimal performance when handling binary data in Node.js applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics