1 | # Behavior of [Structured Clone]
|
2 |
|
3 | [Structured clone] is JavaScript’s algorithm to create “deep copies” of values. It is used for `postMessage()` and therefore is used extensively under the hood with Comlink. By default, every function parameter and function return value is structured cloned. Here is a table of how the structured clone algorithm handles different kinds of values. Or to phrase it differently: If you pass a value from the left side as a parameter into a proxy’d function, the actual function code will get what is listed on the right side.
|
4 |
|
5 | | Input | Output | Notes |
|
6 | | -------------------------- | :------------: | -------------------------------------------------------------------------------------------- |
|
7 | | `[1,2,3]` | `[1,2,3]` | Full copy |
|
8 | | `{a: 1, b: 2}` | `{a: 1, b: 2}` | Full copy |
|
9 | | `{a: 1, b() { return 2; }` | `{a: 1}` | Full copy, functions omitted |
|
10 | | `new MyClass()` | `{...}` | Just the properties |
|
11 | | `Map` | `Map` | [`Map`][map] is structured cloneable |
|
12 | | `Set` | `Set` | [`Set`][set] is structured cloneable |
|
13 | | `ArrayBuffer` | `ArrayBuffer` | [`ArrayBuffer`][arraybuffer] is structured cloneable |
|
14 | | `Uint32Array` | `Uint32Array` | [`Uint32Array`][uint32array] and all the other typed arrays are structured cloneable |
|
15 | | `Event` | ❌ | |
|
16 | | Any DOM element | ❌ | |
|
17 | | `MessagePort` | ❌ | Only transferable, not structured cloneable |
|
18 | | `Request` | ❌ | |
|
19 | | `Response` | ❌ | |
|
20 | | `ReadableStream` | ❌ | [Streams are planned to be transferable][transferable streams], but not structured cloneable |
|
21 |
|
22 | [structured clone]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
|
23 | [map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
24 | [set]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
25 | [arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
26 | [uint32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
|
27 | [transferable streams]: https://github.com/whatwg/streams/blob/master/transferable-streams-explainer.md
|