Back to results for “JSON & Data Serialization”
JavaScript › JSON & Data Serialization

What is structuredClone() and how does it differ from JSON.parse(JSON.stringify(obj)) for deep cloning?

Advanced

Answer

structuredClone() is a native deep-cloning function that correctly handles circular references, Maps, Sets, Dates, TypedArrays, and more — all of which the JSON round-trip technique either throws on or silently mangles. It's the modern, correct default choice for deep cloning.
const original = { date: new Date(), map: new Map([['a', 1]]) };
const clone = structuredClone(original);
console.log(clone.date instanceof Date); // true
console.log(clone.map instanceof Map);   // true
Real-world example Deep-cloning complex application state (including Dates and Maps) without the data-loss bugs of the JSON-based hack.

Common follow-ups: What kinds of values can structuredClone() NOT handle, like functions or DOM nodes?