A real deep copy
The structured clone algorithm is how the platform makes a deep copy of a value. The structuredClone function exposes it directly, and it also runs when you post a message between workers or store data in IndexedDB.
- It copies nested objects, arrays, maps, sets, and dates.
- It preserves cyclic references without infinite loops.
- It produces a fully independent copy with no shared references.
What it cannot copy
The algorithm is not universal. Some values have no meaningful clone and throw a DataCloneError.
- Functions and class instances with custom prototypes cannot be cloned.
- DOM nodes and most platform objects are excluded.
- Property descriptors, getters, and the prototype chain are not preserved.
Compared to JSON parse of JSON stringify, structured clone keeps types like Map and Date and handles cycles, so it is the better default for copying plain data. For passing large buffers between workers, you can mark them as transferable to move ownership instead of copying, which avoids the cost entirely.
Key idea
Structured clone deep copies typed data and cycles where JSON fails, but rejects functions and DOM nodes, and can transfer buffers to skip copying.