structuredClone & Deep Copying
Also Known As
deep clone
deep copy
structuredClone
JSON.parse JSON.stringify
TL;DR
structuredClone() is the modern native way to deep clone JavaScript objects — replacing JSON.parse(JSON.stringify()) and lodash cloneDeep for most use cases.
Explanation
Deep copying creates an independent copy of a nested object — mutations to the copy don't affect the original. Before structuredClone (2022): JSON roundtrip (loses undefined, functions, Dates become strings, no circular refs), Object.assign/spread (shallow only), lodash cloneDeep (dependency). structuredClone handles: Date, Map, Set, ArrayBuffer, RegExp, circular references. Does not handle: functions, DOM nodes, class instances (loses prototype). Available in all modern browsers and Node 17+.
Common Misconception
✗ JSON.parse(JSON.stringify(obj)) is a reliable deep clone — it silently drops undefined values, converts Date to string, loses Map/Set, and throws on circular references.
Why It Matters
Accidental mutation of shared objects is a common JavaScript bug — shallow copies with spread operator look like deep copies but nested objects are still shared references.
Common Mistakes
- Spread operator for nested objects: {...obj} — only shallow; nested objects are still shared.
- JSON roundtrip losing Date objects — Date becomes string, never comes back as Date.
- Not using structuredClone for state management — accidentally mutating shared state causes hard-to-trace bugs.
- structuredClone on objects with functions — functions are not cloneable; they are silently dropped.
Code Examples
✗ Vulnerable
// JSON roundtrip — lossy:
const original = { date: new Date(), value: undefined, map: new Map() };
const clone = JSON.parse(JSON.stringify(original));
// clone.date is a string, not Date
// clone.value is gone (undefined dropped)
// clone.map is {} (Map not supported)
// Shallow copy mutation bug:
const state = { user: { name: 'Alice' } };
const copy = { ...state };
copy.user.name = 'Bob'; // ALSO mutates state.user.name!
✓ Fixed
// structuredClone — handles most types correctly:
const original = { date: new Date(), set: new Set([1,2,3]), arr: [1,[2,3]] };
const clone = structuredClone(original);
clone.date; // Still a Date object ✓
clone.set; // Still a Set ✓
clone.arr[1].push(4); // Does NOT affect original.arr ✓
// For class instances, implement a clone method:
class Order {
clone(): Order {
return Object.assign(new Order(), structuredClone({ ...this }));
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
26
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 6
Amazonbot 6
Google 6
Unknown AI 2
Majestic 1
Ahrefs 1
Also referenced
How they use it
crawler 18
crawler_json 3
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use structuredClone() for deep copying objects and arrays instead of JSON.parse(JSON.stringify()) — it handles Dates, RegExps, Maps, Sets, and circular references that JSON round-trips break
📦 Applies To
javascript ES2021
web
cli
🔗 Prerequisites
🔍 Detection Hints
JSON.parse(JSON.stringify(obj)) for deep clone losing Date types; spread {...obj} for shallow copy assumed deep; missing deep clone causing shared state bugs
Auto-detectable:
✗ No
eslint
typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: Function