structuredClone: Deep Cloning Made Easy
You'll learn how structuredClone creates true deep copies of objects without manual recursion or library dependencies.
For years, JavaScript developers struggled with deep cloning. The spread operator ({...obj}) and Object.assign() only create shallow copies, leaving nested objects as references. Manual recursive functions are error-prone, and JSON.parse(JSON.stringify(obj)) breaks with functions, dates, and circular references.
The Problem structuredClone Solves
Before structuredClone, creating a true deep copy required either:
- Writing custom recursive logic (complex, buggy)
- Using external libraries like Lodash (added dependency)
- Using the JSON trick (loses methods, dates, and more)
How structuredClone Works
structuredClone creates a true deep copy of any structured-cloneable value, handling nested objects, arrays, dates, maps, sets, and even circular references automatically:
const original = {
name: "Alice",
created: new Date(),
nested: {
deep: { value: 42 }
}
};
const clone = structuredClone(original);
// Completely independent
clone.nested.deep.value = 100;
console.log(original.nested.deep.value); // Still 42
// Dates are properly cloned
console.log(clone.created instanceof Date); // true
What Gets Cloned
structuredClone handles most data types: objects, arrays, primitive values, Date, Map, Set, RegExp, ArrayBuffer, and typed arrays. It even preserves circular references. However, it cannot clone functions, DOM nodes, or symbols.
Key Takeaway: structuredClone is the modern, built-in solution for deep cloning in JavaScript—no libraries needed, and it handles edge cases that manual approaches miss.