Buffers Basics
7 examples to get you started with Buffers - 5 basic and 2 intermediate.
Search across all documentation pages
7 examples to get you started with Buffers - 5 basic and 2 intermediate.
Buffer is global; also available via node:buffer.Create fixed-size binary storage with known encoding upfront.
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(8); // zero-filled
buf.write('hello', 0, 'utf8');
console.log(buf.toString('utf8')); // hello with null paddingBuffer.alloc(n) zero-fills - safe for crypto and secrets.Buffer.allocUnsafe(n) is faster but may contain old memory - overwrite fully before use.Buffer.byteLength(str, 'utf8') sizes UTF-8 without allocating.Related: Buffer Security - zero-fill and leaks
Encode text into bytes with an explicit encoding.
const text = 'Node.js 24';
const buf = Buffer.from(text, 'utf8');
console.log(buf.length, Buffer.byteLength(text, 'utf8'));utf8 - specify explicitly in security-sensitive code.TypeError in strict conversions.buf.toString('hex') dumps bytes for debugging protocols.Related: Text Encodings - UTF-8 and malformed input
slice returns a view sharing the same underlying memory.
const original = Buffer.from('abcdef', 'utf8');
const slice = original.subarray(2, 5); // 'cde'
slice[0] = 67; // 0x43 'C'
console.log(original.toString()); // abCdefsubarray is the modern alias - same behavior as slice for Buffers.Buffer.from(slice) when isolating.Binary equality and joining chunks from network reads.
const a = Buffer.from('abc');
const b = Buffer.from('abc');
console.log(a.equals(b)); // true
const combined = Buffer.concat([Buffer.from('hel'), Buffer.from('lo')]);equals is timing-safe enough for length-matched buffers - use crypto.timingSafeEqual for secrets.Buffer.concat(list, totalLength) pre-size when you know final length to avoid realloc.concat in loops - use a list then one concat or a stream.Fine for config and keys; use streams for large assets.
import { readFile } from 'node:fs/promises';
const buf = await readFile('logo.png');
console.log(buf.length, buf[0], buf[1]); // PNG magic bytesbuf.subarray(0, 8) inspects magic numbers for type sniffing.Related: fs and fs/promises - async file I/O
Web APIs and fetch bodies use Uint8Array - Buffers are Uint8Array subclasses.
const buf = Buffer.from([1, 2, 3]);
const view: Uint8Array = buf;
console.log(view instanceof Uint8Array); // true
const copy = Buffer.from(view); // copies when isolation neededArrayBuffer views from WASM or Web Crypto interoperate via .buffer, .byteOffset, .byteLength.Buffer.from(arrayBuffer, byteOffset, length) creates a view-backed Buffer.Common binary framing pattern for TCP protocols.
function encodeFrame(payload: Buffer): Buffer {
const header = Buffer.alloc(4);
header.writeUInt32BE(payload.length);
return Buffer.concat([header, payload]);
}
function decodeFrame(data: Buffer): { frame: Buffer; rest: Buffer } {
const len = data.readUInt32BE(0);
return { frame: data.subarray(4, 4 + len), rest: data.subarray(4 + len) };
}BE) network byte order by convention.Related: Streams Basics - large payload handling
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 19, 2026