← Lessons

quiz vs the machine

Platinum1780

Frontend

Structured Clone

The structured clone algorithm deep copies values and even handles cyclic references.

5 min read · advanced · beat Platinum to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What can structured clone handle that JSON cannot?

2. What happens when you try to clone a function?

3. How can you move a large buffer between workers without copying?