In the programming world, copying data is a common task. However, not all copies are created equal. Two terms that often appear are shallow copy and deep copy. Understanding the difference between them is crucial to avoid errors that can be difficult to detect.
A shallow copy copies only the first level of an object, leaving references to the original data at deeper levels. This means that if the original object has other objects inside it (nested), shallow copy will only copy the references to those objects, not the objects themselves.
const originalArray = [1, 2, [3, 4]]; const shallowCopy = originalArray.slice(); shallowCopy[2][0] = 99; console.log(originalArray); // [1, 2, [99, 4]] console.log(shallowCopy); // [1, 2, [99, 4]]
import copy original_list = [1, 2, [3, 4]] shallow_copy = copy.copy(original_list) shallow_copy[2][0] = 99 print(original_list) # [1, 2, [99, 4]] print(shallow_copy) # [1, 2, [99, 4]]
A shallow copy is useful when you know you don't need to modify nested objects. It is faster and consumes less memory than a deep copy.
In JavaScript, if you use Array.slice() or Object.assign(), you are doing shallow copy!
A deep copy copies all levels of an object, duplicating even nested structures. This means that any changes made to the copy will not affect the original object.
const originalArray = [1, 2, [3, 4]]; const deepCopy = JSON.parse(JSON.stringify(originalArray)); deepCopy[2][0] = 99; console.log(originalArray); // [1, 2, [3, 4]] console.log(deepCopy); // [1, 2, [99, 4]]
import copy original_list = [1, 2, [3, 4]] deep_copy = copy.deepcopy(original_list) deep_copy[2][0] = 99 print(original_list) # [1, 2, [3, 4]] print(deep_copy) # [1, 2, [99, 4]]
If you are working with complex or nested data structures, deep copy is the safest option to avoid unwanted side effects.
In Python, copy.deepcopy() is your friend when you need to safely duplicate complex objects.
Here is a direct comparison between shallow copy and deep copy:
Feature | Shallow Copy | Deep Copy |
---|---|---|
Shallow copy | Yeah | No |
Deep copy | No | Yeah |
Modifications to the original object affect the copy | Yeah | No |
Complexity | Low | High |
Remember, a shallow copy is faster, but a deep copy is safer when working with complex objects.
Shallow copies are great for duplicating lightweight application settings or temporary data!
A common mistake is to use a shallow copy instead of a deep copy when data is nested. This can lead to unwanted modifications to the original object.
const originalArray = [1, 2, [3, 4]]; const shallowCopy = originalArray.slice(); shallowCopy[2][0] = 99; console.log(originalArray); // [1, 2, [99, 4]] (¡No esperado!)
Always check if your object has nested levels before deciding between a shallow or deep copy.
const originalObject = { a: 1, b: { c: 2 } }; const shallowCopy = Object.assign({}, originalObject);
const originalArray = [1, 2, 3]; const shallowCopy = [...originalArray];
const originalObject = { a: 1, b: { c: 2 } }; const deepCopy = structuredClone(originalObject);
structuredClone() is perfect for copying complex or circular structures without breaking your head.
const _ = require('lodash'); const originalObject = { a: 1, b: { c: 2 } }; const deepCopy = _.cloneDeep(originalObject);
import copy original_list = [1, 2, [3, 4]] shallow_copy = copy.copy(original_list) deep_copy = copy.deepcopy(original_list)
In Python, a shallow copy is sometimes all you need to avoid accidental changes to your lists!
In summary, both shallow copies and deep copies have their uses. The key is to understand the structure of the data you are working with and choose the appropriate copy method.
Yes, because it copies less data.
Yes, with JSON.parse(JSON.stringify()) or structuredClone().
The original object will also be affected.
Not necessarily, only when you work with complex data structures.
It is native, supports circular structures and is more efficient than JSON.parse(JSON.stringify()), in addition to allowing values to be completely transferred from one object to another.
Mistakes when using shallow copies instead of deep copies are more common than you think! I hope this little guide helps you avoid any problems when copying data.
Let me know in the comments, did you already know about deep and shallow copies and have you ever had a problem due to them?
Photo by Mohammad Rahmani on Unsplash
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3