Buffers and Encoding
Binary data idioms with Buffer and encodings - the foundation for files, sockets, and crypto. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Binary data idioms with Buffer and encodings - the foundation for files, sockets, and crypto. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Create a buffer from text with an explicit encoding (default utf8).
const buf = Buffer.from("hello", "utf8");
buf.length // 5
buf.toString() // "hello"alloc zero-fills; allocUnsafe is faster but may contain old memory - overwrite before expose.
const safe = Buffer.alloc(4);
safe.equals(Buffer.from([0, 0, 0, 0])) // true
const fast = Buffer.allocUnsafe(4);
fast.fill(0);
fast.length // 4Multibyte UTF-8 characters need more than string.length code units - use Buffer.byteLength.
const s = "café";
s.length // 4
Buffer.byteLength(s, "utf8") // 5subarray (and legacy slice) share memory with the parent - mutating one affects the other.
const parent = Buffer.from("abcdef");
const view = parent.subarray(0, 3);
view[0] = 0x5a; // "Z"
parent.toString() // "Zbcdef"Convert binary to transport-safe strings and back.
const raw = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
raw.toString("hex") // "deadbeef"
raw.toString("base64") // "3q2+7w=="
Buffer.from("deadbeef", "hex").equals(raw) // trueJoin chunks collected from a stream into one buffer.
const parts = [Buffer.from("a"), Buffer.from("b")];
Buffer.concat(parts).toString() // "ab"
Buffer.concat(parts).length // 2Constant-time equality for secrets should use crypto.timingSafeEqual when lengths match.
import { timingSafeEqual } from "node:crypto";
const a = Buffer.from("token");
const b = Buffer.from("token");
timingSafeEqual(a, b) // true
// lengths must match or timingSafeEqual throwsRead and write multi-byte integers at offsets for binary protocols.
const buf = Buffer.alloc(4);
buf.writeUInt32BE(0x12345678, 0);
buf.readUInt32BE(0).toString(16) // "12345678"Buffers are Uint8Array views - share memory with TypedArrays carefully.
const buf = Buffer.from([1, 2, 3, 4]);
buf instanceof Uint8Array // true
buf[0] // 1Web-standard text encoding APIs work in Node for streams of strings.
const enc = new TextEncoder();
const dec = new TextDecoder("utf-8");
const bytes = enc.encode("hi");
dec.decode(bytes) // "hi"
bytes.length // 2Detect Buffer instances when validating untrusted inputs.
import { Buffer } from "node:buffer";
Buffer.isBuffer(Buffer.from("x")) // true
Buffer.isBuffer(Uint8Array.of(1)) // falseFill a range or copy between buffers without string conversion.
const a = Buffer.alloc(4, 0);
const b = Buffer.from("xy");
b.copy(a, 1);
a.toString() // "\0xy\0" (nulls at ends)
a[1] // 120 ("x")Prefer base64url for tokens in URLs (Node encoding support).
const tok = Buffer.from("payload").toString("base64url");
tok // "cGF5bG9hZA"
Buffer.from(tok, "base64url").toString() // "payload"Decode multibyte UTF-8 safely across chunk boundaries with StringDecoder.
import { StringDecoder } from "node:string_decoder";
const dec = new StringDecoder("utf8");
const euro = Buffer.from("€", "utf8"); // 3 bytes
dec.write(euro.subarray(0, 1)) + dec.write(euro.subarray(1))
// "€" (empty first write, complete on second)Web Blob is available for interop with fetch bodies and undici.
const blob = new Blob([Buffer.from("hi")], { type: "text/plain" });
blob.size // 2
await blob.text() // "hi"Stack versions: Node.js 24.18.0 (LTS line 24) · TypeScript 5.6+ · npm 10+
Reviewed by Chris St. John·Last updated Jul 18, 2026