Buffer ownership, transfer and detachment
Quick answer
Binary storage can be copied or transferred. Transfer moves ownership of a transferable buffer and detaches its original storage.
How it works
A host can support structuredClone(value,{transfer:[buffer]}); the buffer in the transfer list becomes detached and its byteLength is zero. A normal structured clone copies a buffer instead. Resizable ArrayBuffer can be created with maxByteLength and resized within its limit in supporting runtimes; fixed-length and resizable buffers have different view behavior. Test support before relying on newer APIs. SharedArrayBuffer is a separate shared-memory type and not transferable.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const b=new Uint8Array([4,5]).buffer;const c=structuredClone(b,{transfer:[b]});console.log(b.byteLength,JSON.stringify([...new Uint8Array(c)]));})()Expected console output:
0 [4,5]The destination retains data while the source is detached.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const b=new ArrayBuffer(2,{maxByteLength:4});b.resize(4);console.log(b.byteLength,b.resizable,JSON.stringify([...new Uint8Array(b)]));})()Expected console output:
4 true [0,0,0,0]Newly grown bytes are initialized to zero.
Common mistakes
Never continue reading the source after transfer. A zero byteLength alone does not prove detachment because a valid empty buffer also has length zero.
Practice
Compare copy and transfer for a three-byte buffer and describe ownership at each step.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Buffer ownership, transfer and detachment — Knowledge Check
Apply the concept and reason about local example output.
Which storage loses its usable bytes after a successful transfer?
What happens when a normal buffer clone is made without a transfer list?
Can byteLength zero by itself establish that a buffer was transferred?
What is the expected console output of Buffer ownership, transfer and detachment example 1?
(async () => {const b=new Uint8Array([4,5]).buffer;const c=structuredClone(b,{transfer:[b]});console.log(b.byteLength,JSON.stringify([...new Uint8Array(c)]));})()What is the expected console output of Buffer ownership, transfer and detachment example 2?
(async () => {const b=new ArrayBuffer(2,{maxByteLength:4});b.resize(4);console.log(b.byteLength,b.resizable,JSON.stringify([...new Uint8Array(b)]));})()