When working with objects and arrays in JavaScript, creating copies of data structures is a common task. However, developers often face challenges when deciding between a shallow copy and a deep copy. Misunderstanding the differences can lead to unintended side effects in your code. Let’s dive into these concepts, their differences, and when to use each.
? Download eBook - JavaScript: from ES2015 to ES2023
A shallow copy creates a new object with copies of the top-level properties of the original object. For properties that are primitives (e.g., numbers, strings, booleans), the value itself is copied. However, for properties that are objects (like arrays or nested objects), only the reference is copied—not the actual data.
This means that while the new object has its own copy of top-level properties, the nested objects or arrays remain shared between the original and the copy.
const original = { name: "Alice", details: { age: 25, city: "Wonderland" } }; // Shallow copy const shallowCopy = { ...original }; // Modify the nested object in the shallow copy shallowCopy.details.city = "Looking Glass"; // Original object is also affected console.log(original.details.city); // Output: "Looking Glass"
const shallowCopy = { ...originalObject };
const shallowCopy = Object.assign({}, originalObject);
While these methods are fast and easy, they are not suitable for deeply nested objects.
A deep copy duplicates every property and sub-property of the original object. This ensures that the copy is completely independent of the original, and changes to the copy do not affect the original object.
Deep copying is essential when dealing with complex data structures like nested objects or arrays, particularly in scenarios where data integrity is critical.
const original = { name: "Alice", details: { age: 25, city: "Wonderland" } }; // Shallow copy const shallowCopy = { ...original }; // Modify the nested object in the shallow copy shallowCopy.details.city = "Looking Glass"; // Original object is also affected console.log(original.details.city); // Output: "Looking Glass"
const shallowCopy = { ...originalObject };
const shallowCopy = Object.assign({}, originalObject);
Feature | Shallow Copy | Deep Copy | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Copies only top-level properties. | Copies all levels, including nested data. | |||||||||||||||
References | Nested references are shared. | Nested references are independent. | |||||||||||||||
Performance | Faster and lightweight. | Slower due to recursive operations. | |||||||||||||||
Use Cases | Flat or minimally nested objects. | Deeply nested objects or immutable structures. |
Copying a user’s settings object to make quick adjustments:
const original = { name: "Alice", details: { age: 25, city: "Wonderland" } }; // Shallow copy const shallowCopy = { ...original }; // Modify the nested object in the shallow copy shallowCopy.details.city = "Looking Glass"; // Original object is also affected console.log(original.details.city); // Output: "Looking Glass"
Duplicating the state of a game or application:
const shallowCopy = { ...originalObject };
Assuming Shallow Copy Is Always Sufficient:
Overusing JSON Methods:
Neglecting Performance:
Understanding the difference between shallow copy and deep copy is essential for writing bug-free JavaScript code. Shallow copies are efficient for flat structures, while deep copies are indispensable for complex, nested objects. Choose the appropriate method based on your data structure and application needs, and avoid potential pitfalls by knowing the limitations of each approach.
? Download eBook - JavaScript: from ES2015 to ES2023
The above is the detailed content of JavaScript Shallow Copy vs Deep Copy: Examples and Best Practices. For more information, please follow other related articles on the PHP Chinese website!