Buffers

⭐ Interview Importance: HIGH
⏱️ Revision Time: 3 min

TL;DR

A Buffer is a global Node.js class used to directly work with fixed-length sequences of bytes (binary data). JavaScript originally had no mechanism for reading or manipulating raw streams of binary data; Buffers were introduced in Node.js to solve this.

Mental Model

How It Works

V8’s memory heap stores JavaScript objects and strings, which is inefficient for massive binary files.
Buffers are allocated outside of the V8 JavaScript heap (in C++ space).
A Buffer represents a fixed chunk of memory. Once it is allocated (e.g., Buffer.alloc(1024) for 1 KB), it cannot be resized.

When you read a file with fs.readFile or receive data over a TCP socket, Node.js gives you a Buffer object.

Example

// 1. Create a buffer from a string
const buf = Buffer.from('Hello World', 'utf8');

// Notice it prints raw hexadecimal bytes
console.log(buf); 
// <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

// 2. Convert it back to a string
console.log(buf.toString('utf8')); 
// "Hello World"

// 3. Allocate an empty buffer of exactly 10 bytes
const emptyBuf = Buffer.alloc(10);

// 4. Working with streams (Chunks are Buffers!)
const fs = require('fs');
fs.createReadStream('video.mp4').on('data', (chunk) => {
    console.log(Buffer.isBuffer(chunk)); // true
    console.log(`Received ${chunk.length} bytes of binary data.`);
});

Common Interview Questions

What is the difference between Buffer.alloc() and Buffer.allocUnsafe()?

Buffer.alloc(size) creates a buffer and initializes all the memory to 0. It is secure but slightly slow.
Buffer.allocUnsafe(size) creates a buffer but does not clear the memory. It is extremely fast, but the buffer might contain sensitive data left over from old programs (passwords, encryption keys). You must ensure you overwrite the entire buffer before returning it to a user.

Why do we need Buffers if ES6 introduced Uint8Array?

Buffers were created in Node.js long before ES6 standardized TypedArrays for the browser. Today, a Node.js Buffer is actually a subclass of JavaScript’s native Uint8Array, meaning they are largely interchangeable, but Buffer provides additional helpful methods (like .toString('base64')).