Course contentsShow
← Web Development
Lesson 2089 of 2,98238. JavaScript Performance in the BrowserFree lesson

Transferable Objects for Zero-Copy Communication

Using ArrayBuffers and other transferables to move data ownership without copying for better performance.

Transferable Objects for Zero-Copy Communication

You'll learn how to transfer ownership of data between threads without copying it, dramatically improving performance for large data transfers.

The Problem with Structured Clone

When you use postMessage to send data to a Web Worker (as we covered in lesson 2088), the browser uses the Structured Clone Algorithm by default. This means it creates a complete copy of your data. For large datasets like images, audio buffers, or video frames, copying can be slow and memory-intensive.

What Are Transferable Objects?

Transferable objects let you transfer ownership of data instead of copying it. Think of it like handing someone the keys to your car instead of building them an identical car. The original thread loses access to the data, but the receiving thread gets it instantly—no copying required.

Which Objects Are Transferable?

Currently, these types support transfer:

  • ArrayBuffer (raw binary data)
  • MessagePort
  • ImageBitmap
  • OffscreenCanvas

How to Transfer

Pass transferable objects as the second argument to postMessage:

// Main thread
const buffer = new ArrayBuffer(1024 * 1024); // 1MB
worker.postMessage({ data: buffer }, [buffer]);
// buffer is now "neutered" - you can't use it anymore!
// Worker
self.onmessage = (e) => {
  const buffer = e.data.data; // Now the worker owns it
  // Process the buffer...
};

The array [buffer] lists which objects to transfer rather than clone.

The Zero-Copy Advantage

For a 100MB buffer, copying might take 50-100ms. Transferring takes microseconds—essentially just moving a pointer. This is critical for real-time applications like video processing or games.

Key Takeaway: Use transferable objects when sending large binary data to Web Workers to avoid expensive copying operations—just remember the original thread loses access to the transferred data.