๐ Definitions#
- Shallow copy
A new object is created, but its nested objects/arrays are still referenced (not copied). - Deep copy
A new object is created, and all nested objects/arrays are recursively copied, so they are fully independent.
๐ฆ Shallow Copy Examples#
const obj = { user: "Ali", settings: { theme: "dark" } };
// Shallow copies
const copy1 = { ...obj };
const copy2 = Object.assign({}, obj);
copy1.user = "Sara";
console.log(obj.user); // "Ali" (independent primitive)
copy1.settings.theme = "light";
console.log(obj.settings.theme); // "light" (nested object was shared!)
๐ Only the top-level properties are copied. Nested references still point to the same objects.
๐ฆ Deep Copy Examples#
Using structuredClone (modern JS)#
const obj = { user: "Ali", settings: { theme: "dark" } };
const deepCopy = structuredClone(obj);
deepCopy.settings.theme = "light";
console.log(obj.settings.theme); // "dark" โ
independent
Using JSON trick (not recommended for all cases)#
const deepCopy = JSON.parse(JSON.stringify(obj));
โ ๏ธ This fails with Date, Map, Set, undefined, functions, circular references.
Using libraries#
- Lodash:
_.cloneDeep(obj) - Immer: handles immutable updates with deep cloning.
๐ Pitfalls of Shallow Copy#
- Mutating nested structures leaks changes:
js
const state = { user: { name: "Ali" } }; const next = { ...state }; next.user.name = "Sara"; console.log(state.user.name); // "Sara" โ unexpected mutation - In React/Redux, this can cause stale renders because React thinks the state is unchanged (reference is same).
โ When to Use#
-
Shallow copy is fine when:
- Object has only primitives.
- You donโt need to mutate nested structures.
- Performance is critical and you control object shape.
-
Deep copy is needed when:
- You must ensure no shared references.
- Working with nested state in frameworks (e.g., Redux).
- Serializing complex objects (careful with special types).
๐ Performance Considerations#
- Deep copy is slower and heavier because it traverses entire object trees.
- Shallow copy is cheap, just reassigns top-level references.
โ Summary#
- Shallow copy: Copies top-level properties only; nested objects are shared.
- Deep copy: Recursively copies everything; no shared references.
- Use shallow copy for flat/simple objects, and deep copy when you need complete independence.
โจ Rule of thumb: If you change a nested value and donโt want it to affect the original โ you need a deep copy.
Start the discussion
Questions, corrections, a better approach? I read and reply to every comment.