What are Buffers in Node.js?
Learn what Node.js Buffers are, how alloc and from work, encodings like hex and base64, and why buffers power files, streams, and network I/O.
Expected Interview Answer
A Buffer is a Node.js class for holding fixed-length sequences of raw binary data outside the V8 heap, letting you work with bytes directly — essential for files, network packets, and streams.
JavaScript strings can't natively represent arbitrary binary data, so Node provides Buffer to store raw bytes. A Buffer is a subclass of Uint8Array with extra methods, allocated with Buffer.alloc(size) (zero-filled and safe) or Buffer.from(data). You can specify encodings like 'utf8', 'hex', or 'base64' when converting between buffers and strings. Buffers have a fixed size once created and are the chunk type that flows through most Node.js streams.
- Handles raw binary data JavaScript strings cannot
- Efficient byte-level manipulation outside the V8 heap
- Supports many encodings (utf8, hex, base64, ascii)
- The native chunk format for streams and I/O
- Interoperates with TypedArrays as a Uint8Array subclass
AI Mentor Explanation
A Buffer is like a fixed row of numbered scorecard boxes on the physical sheet. Each box holds exactly one value, the number of boxes is decided before play starts, and you can read or overwrite any box directly by its position. You cannot magically add extra boxes mid-innings — the row's length is fixed, just as a Buffer's size is set at allocation.
Step-by-Step Explanation
Step 1
Allocate safely
Use Buffer.alloc(size) to get a zero-filled buffer; avoid the unsafe allocUnsafe unless you overwrite it fully.
Step 2
Create from data
Use Buffer.from(string, encoding) or Buffer.from(array) to build a buffer from existing content.
Step 3
Read and write bytes
Index like an array (buf[0]) or use methods like buf.readInt32BE() and buf.write() for typed access.
Step 4
Convert with encodings
Call buf.toString('utf8' | 'hex' | 'base64') to turn bytes into a string in the encoding you need.
Step 5
Combine buffers
Use Buffer.concat([a, b]) to join buffers into a new fixed-size buffer of the total length.
What Interviewer Expects
- Knows a Buffer stores raw binary data outside the V8 heap
- Aware it is a fixed-length Uint8Array subclass
- Can name Buffer.alloc vs Buffer.from and the unsafe variant
- Understands encodings like utf8, hex, and base64
- Connects buffers to streams and file/network I/O
Common Mistakes
- Using deprecated new Buffer() instead of alloc/from
- Using allocUnsafe and leaking old memory contents
- Assuming buffers can grow after creation
- Forgetting to specify an encoding when converting to a string
- Confusing byte length with string character length for multi-byte utf8
Best Answer (HR Friendly)
“A Buffer is Node.js's way of holding raw binary data, like the actual bytes of a file or network message, which regular text strings can't represent. Think of it as a fixed set of numbered boxes for bytes, used whenever the program deals with files, images, or data over the network.”
Code Example
// Safe, zero-filled allocation of 8 bytes
const buf = Buffer.alloc(8)
// Create a buffer from a string (utf8 by default)
const hello = Buffer.from('Hello', 'utf8')
console.log(hello.length) // 5 bytes
console.log(hello[0]) // 72 (byte for 'H')
// Convert between encodings
console.log(hello.toString('hex')) // 48656c6c6f
console.log(hello.toString('base64')) // SGVsbG8=
// Join buffers into one fixed-size buffer
const world = Buffer.from(' World')
const combined = Buffer.concat([hello, world])
console.log(combined.toString()) // Hello WorldFollow-up Questions
- What is the difference between Buffer.alloc and Buffer.allocUnsafe?
- Why is new Buffer() deprecated?
- How does a Buffer relate to Uint8Array?
- How do you convert a Buffer to base64 and back?
- Why can a UTF-8 string's byte length differ from its character count?
MCQ Practice
1. Which method returns a zero-filled, safe buffer?
Buffer.alloc(n) allocates and zero-fills the memory, so it never exposes leftover data.
2. A Node.js Buffer is a subclass of which type?
Buffer extends Uint8Array, adding Node-specific methods for encodings and byte reads/writes.
3. What is true about a Buffer's size after creation?
A Buffer has a fixed length once allocated; to get more space you create a new, larger buffer.
Flash Cards
What is a Buffer used for? — Holding raw binary data (bytes) that JavaScript strings cannot represent, for files and network I/O.
alloc vs allocUnsafe? — alloc zero-fills the memory (safe); allocUnsafe is faster but may expose old memory until overwritten.
What type does Buffer extend? — Uint8Array — a Buffer is a TypedArray with extra Node methods.
Can a Buffer grow? — No — its length is fixed at creation; use Buffer.concat to build a new larger one.