JavaScript Intermediate

Why is structuredClone recommended for deep copying in JavaScript?

AI Practice

What is structuredClone?

structuredClone() is a built-in deep copy API available in browsers and Node.js (v17+), implemented using the Structured Clone Algorithm.

const original = { a: 1, b: { c: [1, 2, 3] }, d: new Date() };
const clone = structuredClone(original);

clone.b.c.push(4);
console.log(original.b.c); // [1, 2, 3] (unaffected)

Why Use structuredClone?

Compared to JSON.parse(JSON.stringify())

Feature JSON method structuredClone
Date Converted to string (broken) ✅ Correctly copied
undefined Lost ✅ Preserved
Map / Set Converted to {} / [] ✅ Correctly copied
RegExp Converted to {} ✅ Correctly copied
ArrayBuffer Cannot handle ✅ Correctly copied
Circular references Throws error ✅ Handled correctly
function Lost ❌ Cannot copy
Symbol Lost ❌ Cannot copy

Example: Date Handling

const obj = { date: new Date() };

// JSON method (broken)
const bad = JSON.parse(JSON.stringify(obj));
console.log(bad.date instanceof Date); // false (became a string)

// structuredClone (correct)
const good = structuredClone(obj);
console.log(good.date instanceof Date); // true

Example: Circular References

const a = { name: 'circular' };
a.self = a; // circular reference

// JSON method
JSON.parse(JSON.stringify(a)); // TypeError: Converting circular structure to JSON

// structuredClone
const clone = structuredClone(a); // handles correctly!

Limitations

  • Cannot clone functions
  • Cannot clone Symbols
  • Cannot clone DOM nodes
  • Cannot clone class instance methods (only copies own properties)

Browser Support

Supported in modern browsers (Chrome 98+, Firefox 94+, Node.js 17+). For older environments, use alternatives like lodash.cloneDeep.

✦ AI Mock Interview

Type your answer and get instant AI feedback

Sign in to use AI scoring

Copyright © 2026 Wood All Rights Reserved · FE Interview Hub