Shallow Copy vs Deep Copy — What's the Difference?
A shallow copy duplicates an object's top-level structure but still shares references to any nested objects inside it; a deep copy recursively duplicates eve...
Published November 23, 2026
A shallow copy duplicates only the top level, so nested objects are still shared with the original; a deep copy duplicates everything recursively so the copy is fully independent.
The concept
Spread syntax, Object.assign and slice() make shallow copies. Changing a nested object in the copy changes the original too.
structuredClone (JS) or copy.deepcopy (Python) produce deep copies, though functions and some special objects may not be cloneable.
Nested data is shared in a shallow copy
orig = { a: 1, tags: ['x'] }
shallow = { ...orig } shallow.tags.push('y')
orig.tags → ['x','y'] (shared!)
Practical examples
Shallow
const copy = { ...orig }
copy.tags.push('y') // also changes orig.tagsDeep
const deep = structuredClone(orig)
deep.tags.push('y') // orig unchangedPython
import copy
shallow = copy.copy(orig)
deep = copy.deepcopy(orig)JSON trick (limited)
JSON.parse(JSON.stringify(obj)) // loses Dates, undefined, functionsCommon mistakes
- Assuming spread makes a deep copy.
- JSON clone losing Dates and functions.
- Deep copying huge structures unnecessarily.
Key takeaways
- Shallow shares nested objects.
- Deep is fully independent.
- Use structuredClone or deepcopy.
FAQ
Which is faster?
Shallow copies, by a lot.
Immutable updates?
Spread only the path you change to avoid deep copies.