GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

The structured clone algorithm

Web Workers API

Web Workers API

The **structured clone algorithm** copies complex JavaScript objects.
It is used internally when invoking structuredClone(), to transfer data between [Workers](/docs/Web/API/Worker) via postMessage(), storing objects with [IndexedDB](/docs/Glossary/IndexedDB), or copying objects for [other APIs](#see_also).

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](/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor), it will be read/write in the duplicate, since that's the default.
- The prototype chain is not walked or duplicated.
- [Class private elements](/docs/Web/JavaScript/Reference/Classes/Private_elements) are not duplicated. (Although internal fields of built-in types may.)

## Supported types

### JavaScript types

- Array
- ArrayBuffer
- Boolean
- DataView
- Date
- Error types (but see [Error types](#error_types) below).
- Map
- Number
- Object objects: but only plain objects (e.g., from object literals).
- [Primitive types](/docs/Web/JavaScript/Guide/Data_structures#primitive_values), except `symbol`.
- RegExp: but note that `lastIndex` is not preserved.
- Set
- String
- TypedArray

#### Error types

For `Error` types, the error name must be one of: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError (or will be set to "Error").

Browsers must serialize the properties `name` and `message`, and are expected to serialize other "interesting" properties of the errors such as `stack`, `cause`, etc.

AggregateError support is expected to be added to the specification in [whatwg/html#5749](https://github.com/whatwg/html/pull/5749) (and is already supported in some browsers).

### Web/API types

- AudioData
- Blob
- CropTarget
- CryptoKey
- DOMException: browsers must serialize the properties name and message. Other attributes may also be serialized/cloned.
- DOMMatrix
- DOMMatrixReadOnly
- DOMPoint
- DOMPointReadOnly
- DOMQuad
- DOMRect
- DOMRectReadOnly
- EncodedAudioChunk
- EncodedVideoChunk
- FencedFrameConfig
- File
- FileList
- FileSystemDirectoryHandle
- FileSystemFileHandle
- FileSystemHandle
- GPUCompilationInfo
- GPUCompilationMessage
- GPUPipelineError
- ImageBitmap
- ImageData
- RTCCertificate
- RTCEncodedAudioFrame
- RTCEncodedVideoFrame
- VideoFrame
- WebTransportError

> [!NOTE]
> Serializable objects are marked up in [Web IDL files](https://github.com/w3c/webref/tree/main/ed/idl) with the attribute `[Serializable]`.

## See also

- [HTML Specification: Safe passing of structured data](https://html.spec.whatwg.org/multipage/infrastructure.html#safe-passing-of-structured-data)
- [Transferable objects](/docs/Web/API/Web_Workers_API/Transferable_objects)
- Window.structuredClone()
- WorkerGlobalScope.structuredClone()
- window.postMessage()
- [Web Workers](/docs/Web/API/Web_Workers_API)
- [IndexedDB](/docs/Web/API/IndexedDB_API)

source: MDN Web Docs mirror · page

Cite this

"The structured clone algorithm". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·