Shallow Copy vs Deep Copy

Shallow Copy vs Deep Copy

When working with objects and arrays in JavaScript, understanding the concepts of shallow copy and deep copy is crucial for effective programming and avoiding unexpected bugs. In this article, we'll delve into what shallow copy and deep copy mean, their differences, and how to perform each type of copy in JavaScript.

Shallow Copy

A shallow copy of an object or array creates a new object or array, but it only copies the references to the original elements. This means that if the elements of the original object or array are themselves objects or arrays, the shallow copy will still refer to the same underlying elements as the original. Let's look at an example:

// Original object
const originalObject = {
  name: 'John',
  age: 30,
  hobbies: ['reading', 'traveling']
};

// Shallow copy
const shallowCopy = Object.assign({}, originalObject);

// Modify the shallow copy
shallowCopy.name = 'Jane';
shallowCopy.hobbies.push('cooking');

console.log(originalObject); // { name: 'John', age: 30, hobbies: [ 'reading', 'traveling', 'cooking' ] }
console.log(shallowCopy); // { name: 'Jane', age: 30, hobbies: [ 'reading', 'traveling', 'cooking' ] }

In the above example, modifying the name property of the shallow copy does not affect the original object. However, modifying the hobbies array (by pushing a new element) affects both the shallow copy and the original object because they share the same reference to the hobbies array.

Deep Copy

A deep copy, on the other hand, creates a completely new object or array with its own copies of all nested elements. This ensures that modifying the copied object or array does not affect the original. Here's how you can create a deep copy in JavaScript:

// Original object
const originalObject = {
  name: 'John',
  age: 30,
  hobbies: ['reading', 'traveling']
};

// Deep copy using JSON
const deepCopy = JSON.parse(JSON.stringify(originalObject));

// Modify the deep copy
deepCopy.name = 'Jane';
deepCopy.hobbies.push('cooking');

console.log(originalObject); // { name: 'John', age: 30, hobbies: [ 'reading', 'traveling' ] }
console.log(deepCopy); // { name: 'Jane', age: 30, hobbies: [ 'reading', 'traveling', 'cooking' ] }

In this example, modifying the name property or the hobbies array of the deep copy does not affect the original object because they are completely separate entities with their own copies of data.

When to Use Shallow Copy vs Deep Copy

  • Shallow Copy: Use shallow copy when you want to create a copy of an object or array and you are sure that the nested elements are immutable or you want changes to propagate to the original object.

  • Deep Copy: Use deep copy when you want to create an independent copy of an object or array, especially when dealing with nested mutable elements or when you want to avoid unintended side effects on the original object.

In conclusion, understanding the differences between shallow copy and deep copy in JavaScript is essential for writing robust and bug-free code. Depending on your use case, choose the appropriate copy method to ensure the desired behavior of your program.

Did you find this article valuable?

Support Manas Upadhyay by becoming a sponsor. Any amount is appreciated!