Deep-copy application data
Create independent state snapshots containing nested objects, arrays, Maps, or Sets.
JavaScript · Function
structuredClone() creates a deep clone of a structured-cloneable JavaScript value. Its optional transfer list can move transferable resources into the clone instead of copying them.
structuredClone(value, options) recursively creates a structured clone of a supported value. With options.transfer, listed transferable resources move into the clone instead of being copied, leaving the original resources detached and unusable.
structuredClone(value)
structuredClone(value, options)| Name | Type | Description |
|---|---|---|
value | any | The value to clone; it must be structured-cloneable. |
options | Object | Optional options object. Its transfer property is an array of transferable objects to move rather than clone. |
A deep copy of the original value.
const original = { user: { name: "Ada" }, values: new Set([1, 2]) };
const copy = structuredClone(original);
copy.user.name = "Grace";
copy.values.add(3);
console.log(original.user.name); // "Ada"
console.log(original.values.has(3)); // falseCreates an independent deep copy, including the nested object and Set.
Create independent state snapshots containing nested objects, arrays, Maps, or Sets.
Move transferable resources such as an ArrayBuffer without duplicating their underlying storage.
Clone cyclic data structures that JSON serialization cannot represent.
It creates a deep clone using the structured clone algorithm, so nested supported values are copied rather than shared. Unlike a shallow copy, changes to nested data in the result do not mutate the source.
Follow-up: How are cycles handled? The algorithm keeps a map of previously visited references while recursing, allowing circular structures without infinite traversal.
The first argument is the value to clone. The optional options object can contain transfer, an array of transferable objects that are moved instead of duplicated.
Follow-up: What happens after transfer? The transferred resource is attached to the clone and detached from the original, so the source can no longer use it.
It returns a deep copy of the input value. If any part is not serializable, it throws a DataCloneError DOMException.
Follow-up: Name two unsupported inputs. Functions and DOM nodes cannot be cloned by the structured clone algorithm.
structured clone algorithmWindow.structuredClone()WorkerGlobalScope.structuredClone()DataCloneErrorTransferable objects