Hiprup

What is structuredClone()?

structuredClone() is a built-in that creates a true deep copy of an object, including nested structures.

  • Deep copy — nested objects and arrays are fully cloned, not shared.

  • Handles more — copies Dates, Maps, Sets, and even circular references, which JSON methods can't.

  • Limits — can't clone functions, DOM nodes, or class prototypes.

Replaces the old JSON.parse(JSON.stringify(obj)) trick, which loses dates, undefined, and special types.

const original = {
  name: 'John',
  date: new Date('2024-01-01'),
  scores: [95, 87, 92],
  nested: { deep: { value: 42 } },
  map: new Map([['key', 'value']]),
  set: new Set([1, 2, 3]),
  regex: /test/gi
};

// structuredClone — handles all types correctly
const deep = structuredClone(original);
deep.nested.deep.value = 99;
console.log(original.nested.deep.value); // 42 — unaffected!
console.log(deep.date instanceof Date);   // true (preserved!)
console.log(deep.map instanceof Map);     // true (preserved!)

// JSON method — loses types
const json = JSON.parse(JSON.stringify(original));
console.log(json.date instanceof Date); // false (it's a string!)
console.log(json.map);                  // {} (Map lost!)
console.log(json.set);                  // {} (Set lost!)
console.log(json.regex);                // {} (RegExp lost!)

// structuredClone limitations
// structuredClone({ fn: () => {} }); // DataCloneError! (functions)
// structuredClone(document.body);     // DataCloneError! (DOM)

structuredClone creates a fully independent deep copy. Date, Map, Set, and RegExp are preserved (unlike JSON which loses them).

Nested objects are independent (modifying the copy does not affect the original). Functions and DOM nodes throw DataCloneError — these cannot be cloned.

structuredClone is the modern answer for deep copying (replacing JSON round-trip and lodash.cloneDeep). Show what it handles that JSON does not: Date, Map, Set, RegExp.

Know its limitations: no functions, no DOM nodes. Available since 2022 in all modern browsers and Node 17+.

What is structuredClone()? | Hiprup