Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Class

Buffer

Buffer represents a fixed-length sequence of bytes in Node.js. It extends Uint8Array with methods for creating, transforming, reading, and writing binary data.

How it works

Buffer represents fixed-length byte sequences and extends Uint8Array with Node.js-specific operations. Create instances with static factories such as Buffer.from or Buffer.alloc, then read, modify, encode, decode, or copy their byte data.

Syntax

Buffer.from(value[, encoding])
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.alloc(size[, fill[, encoding]])
Buffer.allocUnsafe(size)

Parameters

NameTypeDescription
sizeintegerThe number of bytes to allocate for a new Buffer.
valuestring | ArrayLike<number> | ArrayBuffer | BufferThe source array, string, Buffer, or ArrayBuffer used to create the Buffer, depending on the selected Buffer.from overload.
byteOffsetintegerOptional byte offset into an ArrayBuffer.
lengthintegerOptional number of bytes to include from an ArrayBuffer.
fillstring | Buffer | Uint8Array | integerOptional fill value for allocated memory.
encodingstringOptional character encoding used for string input or fill text.

Returns

Buffer.from and Buffer.alloc return Buffer instances; Buffer methods such as toString return values appropriate to the operation.

Key points

  • Buffer is the standard Node.js value for holding binary data with a fixed byte length.
  • Creating from an array copies its numeric byte values, so later array changes do not alter the Buffer.
  • Creating from another Buffer produces independent byte storage.
  • alloc creates a Buffer with the requested size and can initialize it with a fill value using an optional encoding.

Common mistakes

  • Use alloc for initialized memory when contents could be sensitive; allocUnsafe is faster but may expose previously allocated bytes until overwritten.
  • A Buffer made from an ArrayBuffer can share storage, so changes through either view may affect the same bytes.
  • Buffer follows typed-array behavior; code should account for byte-oriented indexing and fixed length rather than treating it like a resizable array.

Example: Encode and decode text with Buffer

import { Buffer } from 'node:buffer';

const original = Buffer.from('hello', 'utf8');
const encoded = original.toString('base64');
const decoded = Buffer.from(encoded, 'base64');

console.log(decoded.toString('utf8')); // hello

Creates a Buffer from UTF-8 text, converts its bytes to Base64, then reconstructs and decodes the bytes.

When to use it

Binary I/O

Handling file, network, or stream payloads that must remain in binary form.

Encoding conversion

Converting text or structured byte data between UTF-8, Base64, and other supported encodings.

API interoperability

Passing byte sequences to Node.js APIs that accept Buffer or Uint8Array values.

Interview questions

QWhat is Buffer, and how does it relate to Uint8Array?

Buffer stores a fixed-length sequence of bytes and is a Uint8Array subclass with additional Node.js methods. Node APIs that accept Buffer also accept ordinary Uint8Array values.

Follow-up: How do Buffer.from(array) and Buffer.from(arrayBuffer) differ? The array form copies the octets, while the ArrayBuffer form can share the underlying memory, optionally using an offset and length.

QHow should a Buffer be created safely?

Use Buffer.from when creating bytes from existing data or text, and Buffer.alloc when requesting initialized memory. Buffer.allocUnsafe can be faster, but its contents must be overwritten before being exposed.

Follow-up: Why is allocUnsafe risky? Newly allocated memory can contain old data, potentially including sensitive information, unless the application writes every relevant byte first.

QHow do encoding and decoding work with Buffer?

Buffer.from(string[, encoding]) encodes a string using the selected encoding, while toString(encoding) decodes bytes back into text. Base64 and UTF-8 are common choices when exchanging binary data through text-oriented interfaces.

Follow-up: What happens when a Buffer is created from an ArrayBuffer? It may share the ArrayBuffer's allocated memory, unlike Buffer.from(buffer), which creates a copy.

  • Buffer.from()
  • Buffer.alloc()
  • Buffer.allocUnsafe()
  • buf.toString()
  • buf.subarray()
  • Uint8Array

Source: Buffer | Node.js v26.11.1 Documentation. Checked 2026-10-15.