Glossary / Structured clone
Structured clone
The structured clone algorithm copies supported values and object graphs for APIs such as structuredClone, worker postMessage, and IndexedDB. It preserves cycles and repeated references within the cloned graph, which JSON round-tripping cannot. It does not preserve arbitrary JavaScript behavior such as functions, accessors, property descriptors, or custom prototypes.
Supported values and transfer
const original = { map: new Map([["x", 1]]) };
const copy = structuredClone(original);
copy.map.get("x"); // 1
Many built-ins are supported, including arrays, plain objects, Maps, Sets, Dates, typed arrays, ArrayBuffers, and cyclic graphs. Functions and DOM nodes fail with DataCloneError; unsupported platform objects may also fail. Property descriptors and getters/setters are not preserved as behavior; cloned ordinary properties are data values. RegExp lastIndex is not preserved.
Transfer lists can move transferable resources such as ArrayBuffers rather than copy them, detaching the original. Transfer is an ownership decision and can invalidate sender-side views. SharedArrayBuffer is shared, not cloned into independent memory. structuredClone is not a universal deep clone for arbitrary class instances and is not a security sanitizer; validate data after crossing a trust boundary.
Interview questions
- Can structured clone preserve cycles? Yes, with graph tracking.
- Can it clone a function? No; it throws DataCloneError.
- Does it preserve custom prototypes/accessors? Generally no.
- What is transfer? Move ownership of a transferable object, often detaching the sender’s copy.