JSintroes2022

structuredClone for deep copies

One call, a real deep copy.

Spreading an object copies only the top layer, so nested values stay shared and mutations leak back. structuredClone walks the whole graph for you — nested objects, Maps, Sets, Dates, even cycles — in a single built-in call, no JSON round-trip and its silent data loss.

The shallow-copy pitfall

JSintro
Press Run to execute

The spread leaks: writing shallow.nested.retries = 99 also shows 99 on the original because both share one nested object. After structuredClone, setting deep.nested.retries = 1 leaves the original at 99 — the nested level is genuinely its own copy.