Skip to content
tezvyn:
Menu

Node.js Buffers: Handling Raw Binary Data

Source: nodejs.orgEasyHow cards are made

A Node.js Buffer is a fixed-size chunk of memory for raw binary data, like an array of bytes. Use it for file I/O or network streams where JS strings fail. The footgun is using allocUnsafe() without overwriting it, which can leak old, sensitive data.

Why it exists

JavaScript's native String type is optimized for Unicode text, not raw binary data. Node.js, as a server environment, needs to read files, handle network packets, and interact with C++ libraries, all of which involve binary data. The Buffer class was created to bridge this gap, providing a way to handle raw byte streams efficiently.

The mental model

A Buffer is a fixed-size slab of memory allocated outside the V8 JavaScript engine's heap. It's like a JavaScript-friendly wrapper around a C++ array of bytes. It represents a sequence of bytes that you can read from or write to as numbers, strings with specific encodings, or other data types.

How it works

When you create a Buffer, for example with Buffer.alloc(8), Node.js reserves 8 bytes of memory and fills it with zeros for safety. You can then write data into it, like buf.write('node'), or write numbers like buf.writeUInt32BE(255, 4). The BE (Big-Endian) and LE (Little-Endian) methods handle byte order, which is critical for network protocols and file formats. You can convert a Buffer back to a string using buf.toString('hex') or buf.toString('utf8').

When to use it

Use Buffers whenever you're dealing with binary data. Common scenarios include reading a file with fs.readFile(), receiving data from a TCP socket, handling image uploads, or performing cryptographic operations which almost always operate on byte arrays.

When not to use it

Avoid using Buffers to store and manipulate text data within your application logic. JavaScript's native String type is far more efficient and safer for handling Unicode characters. Using Buffers for general-purpose string manipulation is an anti-pattern that can lead to bugs and poor performance.

One canonical example

Parsing a custom TCP packet. A server might receive a data chunk where the first 4 bytes are an integer representing the message length, and the rest is a JSON payload. You would use a Buffer to read the length with buf.readUInt32BE(0), then extract and parse the rest of the buffer as a UTF-8 string: buf.toString('utf8', 4, expectedLength). This avoids misinterpreting binary data as text.

Interview question

When would you primarily use a Node.js Buffer?

  • a.As a faster alternative to JavaScript arrays for storing collections of numbers.
  • b.For general-purpose string manipulation and storage within application logic.
  • c.To manage asynchronous operations and callbacks in a non-blocking manner.
  • d.To efficiently process and store raw binary data, such as images or network packets.Correct
Why?

The card states that Buffers are used "whenever you're dealing with binary data," citing examples like "receiving data from a TCP socket, handling image uploads." Option B is explicitly warned against, as "JavaScript's native String type is far more efficient and safer for handling Unicode characters."

Just read this? Test yourself on what you have been reading.

Read the original → nodejs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on node.js — each one lists the topics its interview covers.

See open roles