Skip to content
Node.js: Save Buffer bytes before reusing their parent

Node.js: Save Buffer bytes before reusing their parent

November 15, 2026

A parser can keep a payload view after it has finished reading a packet. If the packet buffer is reused, that view changes too. Copy the selected bytes when handing them to code that needs a stable snapshot.

Set up

Tested on Linux with Node.js v24.22.1. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No package installation or network access is needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as node-buffer-subarray-snapshot.mjs:

import assert from 'node:assert/strict';
import { Buffer } from 'node:buffer';

const packet = Buffer.from([0x01, 0x02, 0x41, 0x42, 0x43]);
const borrowedPayload = packet.subarray(2);
const savedPayload = Buffer.from(borrowedPayload);
const slicedPayload = packet.slice(2);

console.log(`before: borrowed=${borrowedPayload.toString('hex')} saved=${savedPayload.toString('hex')}`);
packet.fill(0);
assert.equal(borrowedPayload.toString('hex'), '000000');
assert.equal(slicedPayload.toString('hex'), '000000');
assert.equal(savedPayload.toString('hex'), '414243');
console.log(`after reuse: borrowed=${borrowedPayload.toString('hex')} saved=${savedPayload.toString('hex')}`);

borrowedPayload[0] = 0x7f;
assert.equal(packet[2], 0x7f);
assert.equal(savedPayload[0], 0x41);
console.log(`view write: packet[2]=${packet[2]} saved[0]=${savedPayload[0]}`);

Run:

node node-buffer-subarray-snapshot.mjs

Observed output:

before: borrowed=414243 saved=414243
after reuse: borrowed=000000 saved=414243
view write: packet[2]=127 saved[0]=65

Read the result

The first two packet bytes are a header; the payload starts at index two. The saved payload owns a copy of the selected bytes. Clearing the packet changes the borrowed payload while leaving the saved copy intact. Writing through the borrowed view also changes the packet.

The assertion for the sliced payload covers a Node-specific pitfall: Buffer slicing also shares bytes. Replacing the subarray call with a Buffer slice call does not create a snapshot.

Apply it carefully

Copy before the producer mutates or reuses the buffer. This example has no concurrent writer; copying shared memory while another thread changes it needs synchronization if a coherent snapshot is required. Copying adds allocation and byte-copying work, so keep a borrowed view when the producer’s lifetime and mutation rules make that safe.

See Node’s Buffer subarray documentation, Buffer copying overload and deprecated Buffer slice behavior.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- node-buffer-subarray-snapshot.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on