Working with Buffers
1. Creating Buffers
| API | Description |
|---|---|
Buffer.from(str, "utf8") | From string |
Buffer.from(arr) | From byte array |
Buffer.from(buf) | Copy |
Buffer.from(arrayBuffer) | Wrap ArrayBuffer (no copy) |
Buffer.alloc(n) | Zero-filled |
Buffer.allocUnsafe(n) | Uninitialized (faster) |
2. Using Buffer.alloc vs Buffer.allocUnsafe
| API | Safety | Speed |
|---|---|---|
alloc | Zero-initialized | Slower |
allocUnsafe | Random memory contents | Faster — must overwrite before reading |
allocUnsafeSlow | Bypasses pool | For long-lived buffers |
3. Writing to Buffers
| Method | Use |
|---|---|
buf.write(str, offset, length, "utf8") | Write string |
buf.writeUInt8(v, off) | 1-byte unsigned |
buf.writeInt32BE/LE | 32-bit signed (BE/LE) |
buf.writeBigInt64BE | 64-bit signed |
buf.fill(value) | Fill bytes |
4. Reading from Buffers
| Method | Returns |
|---|---|
buf.toString("utf8") | Decoded string |
buf.toString("hex") / "base64" | Encoded string |
buf.readUInt8(off) | Byte value |
buf.readInt32BE(off) | 32-bit big-endian |
buf[i] | Index byte access |
5. Converting Encodings
| Encoding | Use |
|---|---|
utf8 | Default text |
utf16le | UTF-16 little-endian |
latin1 | 1 byte/char (binary string) |
ascii | 7-bit only |
base64 / base64url | Binary-safe text |
hex | Two chars per byte |
6. Checking Buffer Length
| API | Returns |
|---|---|
buf.length | Bytes |
Buffer.byteLength(str, "utf8") | Bytes a string would take |
7. Comparing Buffers
| API | Returns |
|---|---|
buf.equals(other) | Boolean |
Buffer.compare(a, b) | -1/0/1 (sortable) |
8. Concatenating Buffers (Buffer.concat)
9. Copying Buffers
| API | Behavior |
|---|---|
buf.copy(target, targetStart, srcStart, srcEnd) | Bytes copied |
Buffer.from(buf) | Full deep copy |
10. Slicing Buffers
| API | Notes |
|---|---|
buf.subarray(start, end) | Shares memory (preferred) |
buf.slice(...) | DEPRECATED — use subarray |
11. Understanding Buffer Pooling
| Concept | Detail |
|---|---|
Buffer.poolSize | Default 8 KB shared pool |
Buffer.allocUnsafe | Uses pool when size < poolSize/2 |
| Long-lived buffers | Use allocUnsafeSlow to avoid keeping pool alive |