KITFORMA · EDITORIAL ANSWER
Why does slicing a Buffer change the original bytes?
KitForma editorial guide. A Buffer view can share the same underlying memory. Editing the view then edits the original allocation rather than an independent copy.
Step-by-step answer
Buffer.from(view) when independent bytes are required, and test by mutating one side after construction. Distinguish Buffer behavior from assumptions based on ordinary JavaScript arrays. Sharing memory is useful for efficient parsing, but ownership must be clear when data crosses asynchronous boundaries. Do not return a mutable view of secret or reusable scratch storage to unrelated callers. Copying adds memory cost, so copy at the boundary that actually requires isolation.
Minimal example:
import assert from 'node:assert/strict';
const original = Buffer.from([1, 2, 3]);
const view = original.subarray(0, 2);
const copy = Buffer.from(view);
view[0] = 9;
assert.equal(original[0], 9);
assert.equal(copy[0], 1);Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.