Shallow Copy vs. Deep Copy โ
Ever wondered how to clone objects in JavaScript without pulling your hair out? ๐ค Well, you're in luck! Today, weโre unpacking the quirks of shallow and deep copying. Buckle up as we explore these essential techniques to avoid the dreaded โunexpected mutationโ fiasco! ๐
Shallow Copy: The Surface Dweller ๐โโ๏ธ โ
A shallow copy is like that friend who only skims the headlines and thinks they know everything. It copies only the top-level elements of an object, leaving nested objects to fend for themselves.
const original = {
name: "Alice",
address: {
city: "Wonderland",
zip: "12345",
},
}
const shallowCopy = { ...original }
shallowCopy.name = "Bob" //Changes only the shallowCopy
shallowCopy.address.city = "New York" // Changes both original and shallowCopy
console.log(original.name) // Alice (unchanged)
console.log(original.address.city) // New York (changed)๐ฑ Wait, what just happened? You changed shallowCopy.address.city, and BAM, original.address.city is also changed. Because our friend shallow copy here just copied the reference to address. It's like moving out but keeping the keys to your old place. ๐ ๐
Methods for Shallow Copy โ
- Spread operator (
...) โ the cool kid ๐ Object.assign()โ the classic gentleman ๐ฉArray.prototype.slice()for arrays โ the slicer ๐Array.from()for arrays โ the formal inviter ๐ง
Deep Copy: The Deep Diver ๐โโ๏ธ โ
Now, letโs talk about deep copy, the one who actually reads the whole article (or object) and makes a complete, independent copy of everything. Nested objects? No problem. Deep copy has got it all covered.
const original = {
name: "Alice",
address: {
city: "Wonderland",
zip: "12345",
},
}
const deepCopy = JSON.parse(JSON.stringify(original))
deepCopy.name = "Bob"
deepCopy.address.city = "New York"
console.log(original.name) // Alice (unchanged)
console.log(original.address.city) // Wonderland (unchanged)๐ฅณ Now thatโs more like it! deepCopy changes do not affect the original object at all. Itโs like moving to a new house and actually handing over your old keys. ๐ก๐ช
Methods for Deep Copy โ
JSON.parse(JSON.stringify())โ the fast but sometimes flaky friend ๐ (note: doesnโt handle functions,undefined, or other non-serializable values)- Libraries like Lodash (
_.cloneDeep()) โ the reliable and trustworthy buddy ๐ค - Custom recursive function โ the DIY enthusiast ๐ ๏ธ
When to Use Which? โ
- Shallow Copy: Use this when youโre in a hurry and just need a quick clone of the top-level properties. Perfect for those โquick fixesโ that always turn into โlong-term problemsโ later. ๐
- Deep Copy: When you need a complete, independent replica, deep copy is your go-to. Ideal for when you donโt want your changes to mess with the original data, especially for complex objects.
๐ Pro Tip โ
๐จ Be cautious with JSON.parse(JSON.stringify()) for large or complex objects. It can be inefficient and sometimes just plain quirky, especially with special object types like Date, Map, Set, and custom objects. ๐งโโ๏ธ
