The structured clone algorithm

The structured clone algorithm copies complex JavaScript objects. It is used internally when invoking structuredClone(), to transfer data between Workers via postMessage(), storing objects with IndexedDB, or copying objects for other APIs.

It clones by recursing through the input object while maintaining a map of previously visited references, to avoid infinitely traversing cycles.

Things that don't work with structured clone

  • Function objects cannot be duplicated by the structured clone algorithm; attempting to throws a DataCloneError exception.
  • Cloning DOM nodes likewise throws a DataCloneError exception.
  • Certain object properties are not preserved:
    • The lastIndex property of RegExp objects is not preserved.
    • Property descriptors, setters, getters, and similar metadata-like features are not duplicated. For example, if an object is marked readonly with a property descriptor, it will be read/write in the duplicate, since that's the default.
    • The prototype chain is not walked or duplicated.

Note: Native Error types can be cloned in Chrome. Firefox can clone DOMException, and is working on the other error types.

Supported types

Object type Notes
All primitive types However, not symbols.
Boolean objects
String objects
RegExp lastIndex is not preserved.
ArrayBufferView Including other typed arrays.
Object Only plain objects (e.g. from object literals)
DOMException Most browsers only clone the properties name and message (in theory stack traces and other attributes may also be cloned).

See also