Worker transfer lists and buffer ownership
Quick answer
A transfer list can move ownership of an ArrayBuffer to a worker instead of cloning its bytes. The original buffer becomes detached.
How it works
With worker.postMessage({buffer}, [buffer]), include the buffer in the data and the transfer list. A typed array is a view, not itself a transferable object; transfer its underlying buffer. A detached buffer reports byteLength zero and existing views cannot be used normally. Transferring the entire underlying buffer may expose bytes outside a small view; copy the intended range first when necessary. SharedArrayBuffer is shared, not transferable.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage([...new Uint8Array(e.data.buffer)]);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const b=new Uint8Array([4,9]).buffer;const result=new Promise((ok,no)=>{w.onmessage=e=>ok(e.data);w.onerror=e=>no(new Error(e.message));});w.postMessage({buffer:b},[b]);console.log(b.byteLength,JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
0 [4,9]The receiver owns the bytes; the sender buffer is detached.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data.byteLength);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const b=new ArrayBuffer(3);const result=new Promise((ok,no)=>{w.onmessage=e=>ok(e.data);w.onerror=e=>no(new Error(e.message));});w.postMessage(b);console.log(b.byteLength,await result);}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
3 3Both the original and cloned buffer retain their lengths.
Common mistakes
Do not reuse the detached sender buffer, include a typed-array view in the transfer list, or assume sending a small subarray transfers only its visible bytes.
Practice
Compare cloned and transferred byte arrays; log the sender byte length after sending and check the worker result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Worker transfer lists and buffer ownership — Knowledge Check
Apply the concept and reason about local example output.
Which item belongs in a transfer list for an ordinary typed array?
What is the sender ArrayBuffer.byteLength after a successful transfer?
How can you limit transfer to a particular view range?
What is the expected console output of Worker transfer lists and buffer ownership example 1?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage([...new Uint8Array(e.data.buffer)]);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const b=new Uint8Array([4,9]).buffer;const result=new Promise((ok,no)=>{w.onmessage=e=>ok(e.data);w.onerror=e=>no(new Error(e.message));});w.postMessage({buffer:b},[b]);console.log(b.byteLength,JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()What is the expected console output of Worker transfer lists and buffer ownership example 2?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data.byteLength);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const b=new ArrayBuffer(3);const result=new Promise((ok,no)=>{w.onmessage=e=>ok(e.data);w.onerror=e=>no(new Error(e.message));});w.postMessage(b);console.log(b.byteLength,await result);}finally{w?.terminate();URL.revokeObjectURL(url);}})()