Transferable Objects
What You'll Learn
You'll learn how to transfer ownership of ArrayBuffers and other transferables for zero-copy messaging between threads, dramatically improving performance when passing large data between workers.
The Zero-Copy Transfer Concept
Normally, when you send data with postMessage to a worker, JavaScript copies the entire data structure. For large ArrayBuffers (like video frames, audio samples, or image data), this copying is expensive and doubles memory usage temporarily.
Transferable objects solve this by transferring ownership instead of copying. Think of it like handing someone the only key to a house rather than building them an identical house next door. After transfer, the original context can no longer access the data—ownership has moved completely.
How It Works
Pass an array of transferable objects as the second argument to postMessage:
// Main thread
const buffer = new ArrayBuffer(1024 * 1024); // 1MB
const uint8 = new Uint8Array(buffer);
uint8[0] = 42;
// Transfer ownership to worker
worker.postMessage({ data: buffer }, [buffer]);
// buffer is now "neutered" - you can't use it here anymore
console.log(buffer.byteLength); // 0
In the worker:
// Worker receives ownership
self.onmessage = (event) => {
const buffer = event.data.data;
console.log(buffer.byteLength); // 1048576
const uint8 = new Uint8Array(buffer);
console.log(uint8[0]); // 42
};
What Can Be Transferred?
ArrayBufferMessagePortImageBitmapOffscreenCanvas
The Trade-off
After transfer, the original object becomes unusable (its byteLength becomes 0). This is intentional—it prevents both contexts from modifying the same memory, avoiding race conditions.
Key Takeaway: Transferable objects enable zero-copy data sharing between workers by moving ownership rather than copying data, providing huge performance gains for large binary data at the cost of losing access in the sender.