The structured clone algorithm
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.
Functionobjects cannot be duplicated by the structured clone algorithm; attempting to throws a
- Cloning DOM nodes likewise throws a
- Certain object properties are not preserved:
RegExpobjects 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.
Errortypes (but see Error types below).
Objectobjects: but only plain objects (e.g. from object literals).
- Primitive types, except
RegExp: but note that
lastIndexis not preserved.
Browsers must serialize the properties
message, and are expected to serialize other "interesting" properties of the errors such as
DOMException: browsers must serialize the properties
message. Other attributes may also be serialized/cloned.