Course contentsShow
← JavaScript
Lesson 2171 of 2,72345. Workers and ConcurrencyFree lesson

The Structured Clone Algorithm

Discover which data types can be sent via postMessage and how they are cloned during transfer.

The Structured Clone Algorithm

You'll learn which data types can safely cross boundaries in postMessage calls and how JavaScript clones them during transfer.

What Is the Structured Clone Algorithm?

When you send data via postMessage (between workers, windows, or threads), JavaScript doesn't just hand over the original object—it creates a deep copy using the Structured Clone Algorithm. This algorithm can clone most JavaScript values while preserving their structure, but it has specific rules about what it can and cannot handle.

Think of it like photocopying a document: you get an exact duplicate, but the copy is completely separate from the original. Changes to one won't affect the other.

What Can Be Cloned?

The algorithm supports:

  • Primitive values (numbers, strings, booleans, null, undefined, BigInt)
  • Plain objects and arrays
  • Date objects
  • RegExp objects
  • Map and Set
  • ArrayBuffer and typed arrays (Uint8Array, etc.)
  • Blob and File objects
  • ImageData

What Cannot Be Cloned?

The algorithm fails on:

  • Functions
  • DOM nodes
  • Symbols
  • Objects with prototype chains (class instances)
  • Getters/setters and non-enumerable properties
  • Objects with circular references are handled, but functions inside them are not

When you attempt to clone unsupported types, JavaScript throws a DataCloneError.

Why This Matters

Understanding the structured clone algorithm prevents runtime errors when passing data between isolated contexts. You know immediately whether your data structure will work with postMessage or if you need to serialize it differently (like converting to JSON first).

Key Takeaway: The Structured Clone Algorithm creates deep copies of most JavaScript data types during postMessage transfers, but cannot clone functions, DOM nodes, or class instances—knowing these boundaries prevents transfer errors.