Structural sharing lets multiple immutable data structures reuse references to unchanged nested data. A shallow copy duplicates only the top level, so shared inner objects avoid unnecessary copying. section 6.13 section 6.14
Use copy-on-write when updating nested data:
- Copy the outer structure, such as an array with slice() or an object with Object.assign(). section 6.14
- Copy and replace only the changed object, leaving unchanged objects shared. section 6.18
- Keep shared data immutable; otherwise the original and updated versions can affect each other. section 6.14
Updating a shopping cart
To change a t-shirt’s price, copy the cart array and the t-shirt object, then replace the item in the copied array. Unchanged shoes and socks remain shared. Only the changed item and the path leading to it require copies. section 6.17 section 6.18


