Node.js Cheatsheet

Buffers

Use this Node.js reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Creating Buffers

// Allocate zeroed buffer (safe — no leaked memory)
const buf = Buffer.alloc(10);            // <Buffer 00 00 00 00 00 00 00 00 00 00>
const buf = Buffer.alloc(10, 0xff);      // filled with 0xff

// Uninitialized (fast but may contain old memory — don't expose raw)
const buf = Buffer.allocUnsafe(10);

// From string
const buf = Buffer.from("hello");                     // default: utf8
const buf = Buffer.from("68656c6c6f", "hex");
const buf = Buffer.from("aGVsbG8=", "base64");
const buf = Buffer.from("hello", "utf8");

// From array of byte values
const buf = Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]);

// From ArrayBuffer / TypedArray
const ab  = new ArrayBuffer(16);
const buf = Buffer.from(ab, 0, 16);     // (arrayBuffer, byteOffset, length)

// Copy of another buffer
const copy = Buffer.from(other);

Never use new Buffer() — deprecated and insecure. Always use Buffer.alloc, Buffer.allocUnsafe, or Buffer.from.

Buffer Properties and Indexing

const buf = Buffer.from("Hello");

buf.length;           // 5 (bytes, NOT characters)
buf[0];               // 72  (byte value)
buf[0] = 104;         // mutates the buffer in place

buf.buffer;           // underlying ArrayBuffer
buf.byteOffset;       // offset into the ArrayBuffer

Encodings

NameDescription
"utf8" (default)Variable-width Unicode
"utf16le"2-byte little-endian Unicode
"latin1" / "binary"ISO-8859-1 — one byte per char
"ascii"7-bit ASCII (high bits stripped)
"base64"Base64 encoding
"base64url"URL-safe Base64 (no +, /, =)
"hex"Hexadecimal string

Converting Buffers

const buf = Buffer.from("Hello, World!");

buf.toString();                   // "Hello, World!"  (utf8)
buf.toString("hex");              // "48656c6c6f..."
buf.toString("base64");           // "SGVsbG8sIFdvcmxkIQ=="
buf.toString("utf8", 0, 5);      // "Hello" (slice before decode)

// To JSON
JSON.stringify(buf);
// {"type":"Buffer","data":[72,101,108,108,111,...]}

// To TypedArray (zero-copy share of same memory)
const u8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);

// To ArrayBuffer (copy)
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);

Copying and Slicing

const buf = Buffer.from("Hello World");

// slice — returns view (same memory, not a copy)
const view = buf.slice(0, 5);   // deprecated alias
const view = buf.subarray(0, 5); // preferred

// copy — writes into another buffer
const dst = Buffer.alloc(5);
buf.copy(dst, 0, 0, 5);  // (target, targetStart, sourceStart, sourceEnd)

// Actual copy
const copy = Buffer.from(buf);           // full copy
const copy = buf.subarray(0, 5);        // view (mutates original!)
const copy = Buffer.allocUnsafe(5);
buf.copy(copy, 0, 0, 5);               // true copy

Reading and Writing Numbers

const buf = Buffer.alloc(8);

// Write integers (offset defaults to 0)
buf.writeUInt8(255, 0);
buf.writeUInt16BE(1000, 0);      // big-endian
buf.writeUInt16LE(1000, 0);      // little-endian
buf.writeUInt32BE(123456, 0);
buf.writeUInt32LE(123456, 0);
buf.writeInt8(-1, 0);
buf.writeInt16BE(-1000, 0);
buf.writeInt32BE(-123456, 0);
buf.writeBigUInt64BE(BigInt("9007199254740993"), 0);
buf.writeBigInt64LE(-1n, 0);
buf.writeFloatBE(3.14, 0);
buf.writeDoubleBE(3.14159265, 0);

// Read integers
buf.readUInt8(0);
buf.readUInt16BE(0);
buf.readUInt32LE(0);
buf.readInt16BE(0);
buf.readBigUInt64BE(0);
buf.readFloatBE(0);
buf.readDoubleBE(0);

Comparing and Searching

const a = Buffer.from("abc");
const b = Buffer.from("bcd");

// Compare (for sorting)
Buffer.compare(a, b);   // -1 | 0 | 1
a.compare(b);           // same
a.compare(b, 0, 2, 0, 2); // compare sub-ranges

// Equality
a.equals(b);            // false
a.equals(Buffer.from("abc")); // true

// Search
const buf = Buffer.from("hello world hello");
buf.indexOf("hello");   // 0
buf.lastIndexOf("hello"); // 12
buf.indexOf(0x6f);      // byte value — finds 'o' at index 4
buf.includes("world");  // true

Concatenating Buffers

const parts = [Buffer.from("foo"), Buffer.from("bar"), Buffer.from("baz")];

// Efficient — allocates once
const result = Buffer.concat(parts);
const result = Buffer.concat(parts, 9);  // explicit total length (skip measuring)

// AVOID in loops:
// let buf = Buffer.alloc(0);
// for (...) buf = Buffer.concat([buf, chunk]);  // O(n²) copies

Filling Buffers

const buf = Buffer.alloc(10);

buf.fill(0);                   // zero everything
buf.fill(0xff);                // fill with 0xff
buf.fill("a");                 // fill with char code of 'a'
buf.fill("ab");                // repeat "ab"
buf.fill(42, 5, 10);          // fill range [5, 10)
buf.fill("hello", 0, 5, "utf8");

Buffer and Streams

// Streams produce Buffer chunks by default (unless encoding set)
for await (const chunk of readableStream) {
  // chunk is a Buffer
  console.log(chunk.toString("utf8"));
}

// Set encoding on a readable to get strings directly
readableStream.setEncoding("utf8");
for await (const str of readableStream) {
  console.log(str);  // string, not Buffer
}

Useful Buffer Patterns

// Hex string from random bytes
import crypto from "node:crypto";
const token = crypto.randomBytes(32).toString("hex");  // 64-char hex

// Base64url token (URL-safe, no padding)
const token = crypto.randomBytes(32).toString("base64url");

// XOR two buffers
function xor(a, b) {
  const result = Buffer.allocUnsafe(a.length);
  for (let i = 0; i < a.length; i++) result[i] = a[i] ^ b[i];
  return result;
}

// Check if buffer is valid UTF-8
import { StringDecoder } from "node:string_decoder";
const decoder = new StringDecoder("utf8");
const str = decoder.write(buf);      // handles partial multi-byte sequences
const tail = decoder.end();          // flush remainder

// Stream text line by line with StringDecoder
const sd = new StringDecoder("utf8");
let remainder = "";
for await (const chunk of stream) {
  const str = remainder + sd.write(chunk);
  const lines = str.split("\n");
  remainder = lines.pop();
  for (const line of lines) console.log(line);
}