Binary I/O
Handling file, network, or stream payloads that must remain in binary form.
Node.js · Class
Buffer represents a fixed-length sequence of bytes in Node.js. It extends Uint8Array with methods for creating, transforming, reading, and writing binary data.
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.
Buffer.from(value[, encoding])
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.alloc(size[, fill[, encoding]])
Buffer.allocUnsafe(size)| Name | Type | Description |
|---|---|---|
size | integer | The number of bytes to allocate for a new Buffer. |
value | string | ArrayLike<number> | ArrayBuffer | Buffer | The source array, string, Buffer, or ArrayBuffer used to create the Buffer, depending on the selected Buffer.from overload. |
byteOffset | integer | Optional byte offset into an ArrayBuffer. |
length | integer | Optional number of bytes to include from an ArrayBuffer. |
fill | string | Buffer | Uint8Array | integer | Optional fill value for allocated memory. |
encoding | string | Optional character encoding used for string input or fill text. |
Buffer.from and Buffer.alloc return Buffer instances; Buffer methods such as toString return values appropriate to the operation.
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')); // helloCreates a Buffer from UTF-8 text, converts its bytes to Base64, then reconstructs and decodes the bytes.
Handling file, network, or stream payloads that must remain in binary form.
Converting text or structured byte data between UTF-8, Base64, and other supported encodings.
Passing byte sequences to Node.js APIs that accept Buffer or Uint8Array values.
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.
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.
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