Behavior of [Structured Clone]

May 5, 2019 · View on GitHub

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.

InputOutputNotes
[1,2,3][1,2,3]Full copy
{a: 1, b: 2}{a: 1, b: 2}Full copy
{a: 1, b() { return 2; }{a: 1}Full copy, functions omitted
new MyClass(){...}Just the properties
MapMapMap is structured cloneable
SetSetSet is structured cloneable
ArrayBufferArrayBufferArrayBuffer is structured cloneable
Uint32ArrayUint32ArrayUint32Array and all the other typed arrays are structured cloneable
Event
Any DOM element
MessagePortOnly transferable, not structured cloneable
Request
Response
ReadableStreamStreams are planned to be transferable, but not structured cloneable