422 words
2 minutes

structuredClone Copies the Whole Typed Array Buffer: Copy Just the View

2026-11-19
Frontend
JavaScript
/
Node.js
/
Troubleshooting
Contents

If you pass a two-byte Uint8Array subarray to structuredClone(), the result still has a two-byte view. Its backing buffer can be much larger. If you then give a consumer access to result.buffer, they can read bytes outside that view. To copy just the visible bytes of a plain Uint8Array, call view.slice().

Check the view and the backing buffer#

Run this as an ES module in Node.js. The fixture was checked with Node.js 24.13.0 on macOS arm64 using a fixed-length ArrayBuffer and ordinary Uint8Array views.

import assert from 'node:assert/strict';
const source = Uint8Array.of(10, 20, 30, 40, 50, 60);
const view = source.subarray(2, 4);
const sibling = source.subarray(4);
const cloned = structuredClone(view);
const compact = view.slice();
const snapshot = structuredClone(compact, { transfer: [compact.buffer] });
assert.equal(compact.byteLength, 0);
assert.deepEqual([...source], [10, 20, 30, 40, 50, 60]);
const moved = structuredClone(view, { transfer: [view.buffer] });
assert.deepEqual(
[source.byteLength, view.byteLength, sibling.byteLength],
[0, 0, 0],
);
const inspect = (bytes) => [
bytes.byteOffset,
bytes.byteLength,
bytes.buffer.byteLength,
[...new Uint8Array(bytes.buffer)],
];
const results = [cloned, snapshot, moved].map(inspect);
assert.deepEqual(results, [
[2, 2, 6, [10, 20, 30, 40, 50, 60]],
[0, 2, 2, [30, 40]],
[2, 2, 6, [10, 20, 30, 40, 50, 60]],
]);
console.log(JSON.stringify(results));

Each result contains the view offset, view length, backing buffer length, and all bytes reachable through the backing buffer.

OperationView offsetBacking bytesEffect on views of the original buffer
Clone the subarray210, 20, 30, 40, 50, 60Remain usable
Copy with slice(), then transfer the copy030, 40Remain usable
Transfer the subarray’s backing buffer210, 20, 30, 40, 50, 60Become detached

The HTML Standard serializes an array-buffer view through its backing buffer and records the view’s offset and length. In the transfer case, detachment affects the underlying buffer, including the sibling view that never appears in the clone input. ECMAScript specifies that TypedArray.prototype.slice() copies the selected elements into a new typed array. HTML structured data · ECMAScript typed array slice

Pick the buffer you intend to hand off#

Use view.slice() when the recipient should get only the selected bytes. If the handoff accepts a transfer list, transfer the copy’s buffer and keep using the original views. Transfer the original buffer when the recipient needs the full allocation and the sender can give up all views of it.

For a Node.js Buffer, use new Uint8Array(view) to make a plain typed-array copy. Node’s Buffer.prototype.slice() creates a view over the same memory. The fixture covers plain typed arrays, and the Buffer distinction comes from the Node.js Buffer documentation.

If cloning throws before you can inspect the byte lengths, check whether the payload contains functions that trigger DataCloneError.

References: HTML structured serialization and transfer · ECMAScript typed array slice · Node.js Buffer

structuredClone Copies the Whole Typed Array Buffer: Copy Just the View
https://laplusda.com/en/posts/structuredclone-typed-array-subarray-buffer/
Author
Zero
Published at
2026-11-19
License
CC BY-NC-SA 4.0