Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript Snippets › structuredClone()

JavaScript Snippet

JavaScript structuredClone()

The native deep-copy function that finally replaces JSON.parse(JSON.stringify()).

Last updated: October 2026

Quick answer: Use structuredClone(obj) to create a true deep copy of any object or array. It handles Date, Map, Set, RegExp, and cyclic references — all the cases the old JSON workaround breaks on. It cannot copy functions or DOM nodes.

🎬 Interactive Live Demo

Click any method to see what it does to nested objects, dates, and methods.

💡 Try all three methods. Watch how shallow copy shares nested objects, JSON loses Dates, and structuredClone preserves everything except functions.

1

The Problem: Shallow Copy Shares References

The spread operator and Object.assign create shallow copies. Nested objects are still shared by reference:

const original = { name: "Alice", address: { city: "Karachi" } };
const copy = { ...original };

copy.address.city = "Lahore";

console.log(original.address.city); // "Lahore" 😱 — original mutated!

The top-level name is independent. But address points to the same object in memory — mutating it in the copy mutates the original.

2

The Old Workaround (and Why It's Broken)

For years, developers used JSON round-tripping as a deep-copy hack:

const copy = JSON.parse(JSON.stringify(original));

It works for plain objects and arrays. But it silently breaks on:

  • Dates — become strings, lose Date methods.
  • Functions — silently dropped.
  • undefined — removed from objects and converted to null in arrays.
  • Map, Set, RegExp — lost entirely.
  • Circular references — throws TypeError: Converting circular structure to JSON.

Worse: the brokenness is silent. No error is thrown. The data just quietly changes shape.

3

The Solution: structuredClone()

const original = {
    name: "Alice",
    createdAt: new Date(),
    address: { city: "Karachi" },
    tags: ["admin", "user"]
};

const copy = structuredClone(original);

copy.address.city = "Lahore";

console.log(original.address.city); // "Karachi" ✅
console.log(copy.createdAt instanceof Date); // true ✅

One function call. True deep copy. No dependencies. No silent data corruption.

4

What structuredClone Can and Cannot Copy

Type Supported
Plain objects & arrays ✅ Yes
Date ✅ Yes
Map, Set ✅ Yes
RegExp ✅ Yes
ArrayBuffer, TypedArray ✅ Yes
Circular references ✅ Yes
Functions ❌ Throws DataCloneError
DOM nodes ❌ Throws DataCloneError
Class instances (with methods) ❌ Prototype lost

Class instances are trickier: structuredClone copies the own properties but drops the prototype — the result is a plain object, not a class instance.

5

Real-World Use Case: State Snapshots

A common pattern — take a snapshot of state before mutating:

// State management pattern
const state = {
    user: { name: "Alice", prefs: { theme: "dark" } },
    cart: [{ id: 1, qty: 2 }]
};

// Before an undo-able action, take a snapshot
const snapshot = structuredClone(state);

// Mutate state
state.user.prefs.theme = "light";
state.cart.push({ id: 2, qty: 1 });

// Later, restore
state = structuredClone(snapshot);

This is cleaner and safer than spreading deeply nested objects, and it handles circular references (which many state shapes contain).

🎯 structuredClone vs The Alternatives

Method Depth Dates Dependencies
Spread {...obj} Shallow Shared None
JSON.parse(JSON.stringify()) Deep Broken None
Lodash cloneDeep Deep Preserved Lodash (~24KB)
structuredClone Deep Preserved None (native)

For most modern apps, structuredClone replaces both the JSON hack and Lodash's cloneDeep.

⚠️ Common Mistakes

❓ Frequently Asked Questions

What is structuredClone in JavaScript?

structuredClone is a built-in JavaScript function that creates a deep copy of an object or array. It handles Date, Map, Set, RegExp, ArrayBuffer, and cyclic references — all the cases JSON.parse(JSON.stringify()) fails on.

What is the difference between shallow copy and deep copy?

A shallow copy copies the top-level structure but nested objects are shared by reference — mutating them affects the original. A deep copy recursively copies everything, so nested changes don't affect the source.

Is structuredClone faster than JSON.parse(JSON.stringify())?

Yes. structuredClone is native and typically 2-5x faster than JSON round-tripping on modern JavaScript engines. It also avoids the string serialization overhead entirely.

What values can't structuredClone handle?

structuredClone throws a DataCloneError for functions, DOM nodes, Symbols, and objects containing these values. It also can't copy class instances with methods or prototype chains.

Does structuredClone handle circular references?

Yes. Unlike JSON.stringify which throws on circular references, structuredClone handles them correctly. This is one of its biggest advantages over JSON.parse(JSON.stringify()).

🎯 What's Next?

← All JavaScript Snippets