JavaScript › JSON & Data Serialization
What is structuredClone() and how does it differ from JSON.parse(JSON.stringify(obj)) for deep cloning?
AdvancedAnswer
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); // trueReal-world example Deep-cloning complex application state (including Dates and Maps) without the data-loss bugs of the JSON-based hack.