← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · ✎ updated 7d
← Back to glossary

structuredClone & Deep Copying

JavaScript ES2021 Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). ESLint and TypeScript can detect some patterns (JSON.parse/stringify antipattern, spread operator on nested objects) but require custom rules or plugins; the detection is not default and misuse often slips through to runtime.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is direct: replace JSON.parse(JSON.stringify(obj)) with structuredClone(obj), or replace {...obj} with structuredClone(obj) for nested cases. This is a straightforward swap within a single line or small refactor, no cross-file changes needed.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice to use shallow vs. deep cloning is localized to where objects are copied; it doesn't reshape the entire codebase or become a system-wide gravitational pattern. However, once a choice is made consistently, future maintainers must understand the cloning strategy in each module.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception is canonical: developers assume JSON.parse(JSON.stringify(obj)) is a reliable deep clone and don't expect it to silently drop undefined, convert Dates to strings, lose Map/Set, or fail on circular refs. The spread operator {...obj} looks syntactically similar to deep-copy patterns in other languages, creating a contradiction against expected behaviour.

About DEBT scoring →

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 }));
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 215
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 1 ping S 1 ping M 0 pings T 2 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 17 Google 15 Scrapy 12 SEMrush 11 Ahrefs 10 Bing 9 Perplexity 8 ChatGPT 7 Unknown AI 4 PetalBot 4 Majestic 2 Claude 2 Sogou 2 Applebot 2 DuckDuckGo 2 Meta AI 1 Twitter/X 1 Baidu 1
crawler 100 crawler_json 8 your_contextpost 1 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
⚡ 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


✓ schema.org compliant