Lesson 63 of 296
Shallow vs deep copy and structuredClone
What you will learn
- The difference between a shallow and a deep copy
- Ways to make shallow copies
- Ways to make deep copies, and their limits
structuredClone- When you need each one
On this page
Shallow copy
A shallow copy creates a new outer object or array, but nested objects inside are still shared with the original.
const original = {
name: "Sam",
address: { city: "Leeds" }
};
const copy = { ...original };
copy.name = "Ali";
copy.address.city = "York";
original.name "Sam" (top level was copied)
original.address.city "York" (nested object is shared!)Only the top level is new. copy.address and original.address point to the same object.
Ways to make a shallow copy
| Arrays | Objects |
|---|---|
[...arr] | { ...obj } |
arr.slice() | Object.assign({}, obj) |
Array.from(arr) |
Deep copy
A deep copy copies every level. Nothing is shared.
structuredClone (the modern way)
const deep = structuredClone(original);
deep.address.city = "Hull";
original.address.city unchangedstructuredClone is built into all modern browsers and Node.js 17+.
What it handles well:
- Nested objects and arrays
Date,Map,Set,RegExp, typed arrays- Circular references (an object that refers to itself)
undefined,NaN,Infinity
What it cannot clone:
- Functions: throws a
DataCloneError - DOM elements
- The prototype: class instances become plain objects and lose their methods
- Getters and setters (it copies their current value)
- Symbols as keys are skipped
The JSON trick (old way)
const deep = JSON.parse(JSON.stringify(original));It works for simple data, but has serious limits:
| Value | After the JSON trick |
|---|---|
Date | Becomes a string |
undefined | Property removed |
| Function | Property removed |
Map, Set | Become {} |
NaN, Infinity | Become null |
| Circular reference | Throws an error |
| BigInt | Throws an error |
Prefer structuredClone in modern code.
Writing your own deep clone
"Implement deep clone" is a common interview task (Part 6). The basic idea is recursion:
function deepClone(value) {
if (value === null || typeof value !== "object") {
return value;
}
if (Array.isArray(value)) {
return value.map(deepClone);
}
const result = {};
for (const [key, val] of Object.entries(value)) {
result[key] = deepClone(val);
}
return result;
}A complete version also handles Date, Map, Set and circular references (using a WeakMap to remember objects already copied).
When do you need a deep copy?
Less often than people think.
- For updating state in React, you usually make targeted shallow copies at each level you change:
const updated = {
...user,
address: { ...user.address, city: "York" }
};This is faster than a deep copy, and unchanged parts keep their references (which helps React skip work).
- Use a deep copy when you must hand data to code that may change it, or keep a snapshot (for example "undo" history).
Common mistakes
- Thinking spread or
Object.assignmakes a deep copy. - Using the JSON trick on data with dates.
- Using
structuredCloneon objects that contain functions or class instances with methods. - Deep cloning large objects when a targeted shallow update is enough.
How to say it in an interview
"A shallow copy creates a new top-level container but nested objects are still shared; spread, Object.assign and slice are shallow. A deep copy duplicates every level. The modern way is structuredClone, which handles dates, maps, sets and circular references but cannot clone functions and loses class prototypes. The JSON trick breaks dates, drops undefined and functions, and fails on circular references. For React state I usually make targeted shallow copies of only the levels I change."
Key words
| Word | Meaning |
|---|---|
| Shallow copy | Only the top level is new; nested objects are shared |
| Deep copy | Every level is copied |
| structuredClone | A built-in function for deep copies |
| Circular reference | An object that refers to itself, directly or indirectly |
| Recursion | A function that calls itself |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.