All posts
bash2 min read

Shallow vs. Deep Copy: Understanding the Difference

When working with objects or data structures in programming, copying data isn't always straightforward.

Introduction

When working with objects or data structures in programming, copying data isn't always straightforward. There are two types of copies: shallow copy and deep copy, and understanding their differences is crucial to avoiding unintended side effects in your code.

Shallow Copy

A shallow copy creates a new object, but it only copies references to the nested objects rather than duplicating them. This means changes to the nested objects in the copy will affect the original.

Example in JavaScript:

const original = { a: 1, b: { c: 2 } };
const shallowCopy = { ...original };

shallowCopy.b.c = 42; 
console.log(original.b.c); // 42 (Changes reflected in original)

Use Case: Suitable for flat objects or when you don’t need to modify nested structures.

Deep Copy

A deep copy creates a completely independent duplicate, including all nested objects. Changes to the copied object won’t affect the original. Example in JavaScript (Using JSON Method):

const original = { a: 1, b: { c: 2 } };
const deepCopy = JSON.parse(JSON.stringify(original));

deepCopy.b.c = 42;
console.log(original.b.c); // 2 (Original remains unchanged)

Use Case: Essential when working with complex objects or preventing accidental mutations.

Choosing the Right Approach

  • Use shallow copy when dealing with simple, non-nested data.
  • Use deep copy when working with deeply nested structures or immutable data.

Which one do you use the most in your projects? 🚀 Let me know!