structuredClone()

Deep-copies a value using the structured clone algorithm — handles plain data, Map, Set, and Date, but not functions or DOM nodes.

                structuredClone(value)
              

Purpose

structuredClone(value) produces a deep copy of a value — unlike a shallow spread (recall shallow copy), nested objects and arrays are copied at every level, not shared with the original.

Syntax

const original = { user: { name: 'Ada', tags: ['engineer'] } };
const clone = structuredClone(original);

clone.user.tags.push('mathematician');
console.log(original.user.tags); // ["engineer"] — untouched, unlike a shallow spread

Behavior notes

  • Supports plain objects and arrays, Map, Set, Date, RegExp, typed arrays, and several other built-in types — a broader set than JSON.stringify/JSON.parse (which drops undefined and functions, and mishandles Date).
  • Throws on functions, DOM nodes, and a few other unsupported types — it is not a universal deep clone for any value.
  • Circular references are supported (unlike JSON.stringify, which throws on them).
  • Available as a global in both browsers and Node (18.11+) — no import needed.

Common mistakes

  • Assuming structuredClone can copy a value containing a function (a class instance with methods on its prototype is fine — the methods live on the prototype, not the instance itself — but a plain object with a function property is not).
  • Reaching for structuredClone for a shallow copy where spread already suffices — it’s more expensive than a shallow copy and only needed when nested structures must be genuinely independent.

Runtime notes

Standardized (the “structured clone algorithm” originates from the HTML/Web Workers spec), available in every current browser and in Node 18.11 and later.

Taught in these lessons

Related reference

Updated 2026-09-03