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

Structured Clone Algorithm and Data Transfer

What data can be sent to workers, how objects are cloned, and why functions cannot be transferred.

Structured Clone Algorithm and Data Transfer

You'll learn what data can safely cross the worker boundary and why some values can't make the trip.


When you use postMessage() to send data between your main thread and a Web Worker, the browser doesn't just pass a reference to your object—it creates a deep copy using the Structured Clone Algorithm. Think of it like photocopying a document: the worker gets its own complete copy, not a shared version.

What Can Be Cloned?

The Structured Clone Algorithm supports most common JavaScript values:

  • Primitives: strings, numbers, booleans, null, undefined
  • Objects and Arrays: plain objects, arrays, nested structures
  • Built-in types: Date, RegExp, Map, Set, ArrayBuffer, Blob, File
  • Typed arrays: Uint8Array, Float32Array, etc.
worker.postMessage({
  name: 'Alice',
  scores: [10, 20, 30],
  timestamp: new Date(),
  data: new Uint8Array([1, 2, 3])
}); // ✅ All cloneable

What Cannot Be Cloned?

Here's the key limitation: functions cannot be transferred. Neither can DOM nodes, symbols, or objects with methods (since methods are functions).

worker.postMessage({
  calculate: function() { return 42; }
}); // ❌ Error: functions aren't cloneable

Why? Functions contain references to their surrounding scope (closures), which may include variables that don't exist in the worker context. Rather than risk broken references, the browser refuses to clone them.

The Cloning Cost

Deep cloning takes time proportional to data size. For large ArrayBuffer objects, you can use transferable objects (covered in advanced topics) to move ownership without copying.

Key Takeaway: The Structured Clone Algorithm creates independent copies of most JavaScript data types when passing messages to workers, but functions and DOM nodes cannot cross the worker boundary because they depend on context that doesn't exist in the worker thread.