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 aDataCloneError
exception. - Cloning DOM nodes likewise throws a
DataCloneError
exception. - Certain object properties are not preserved:
- The
lastIndex
property ofRegExp
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.
- The
Note: Native Error
types can be cloned in Chrome, and Firefox is working on it.
Supported types
Object type | Notes |
---|---|
All primitive types | However, not symbols. |
Boolean objects | |
String objects | |
Date | |
RegExp |
lastIndex is not preserved. |
Blob | |
File | |
FileList | |
ArrayBuffer | |
ArrayBufferView | Including other typed arrays. |
ImageBitmap | |
ImageData | |
Array | |
Object | Only plain objects (e.g. from object literals) |
Map | |
Set |
See also
- HTML Specification: Safe passing of structured data
- Transferable objects
structuredClone()
window.history
window.postMessage()
- Web Workers
- IndexedDB
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm