4 ms·
If you find yourself deep copying then you're about to level up your JavaScript career. The actual answer is not to find out how to deep copy reliably. The ac
by andrewstuart 9y ago
If you find yourself deep copying then you're about to level up your JavaScript career.
The actual answer is not to find out how to deep copy reliably.
The actual answer is don't deep copy - instead you should be using immutable data structure - check out immutablejs. This will solve the problems that you are trying to address by deep copying.
Sticking with deep copying will only increase your pain and decrease your software reliability.
- acemarke 9y agoYeah, generally what's needed for data updates is _nested shallow copies_, not a deep copy. I talked about this in the "Immutable Update Patterns" section of the Redux docs [0], and the articles in the "Immutable Data" section of my React/Redux links list [1] give more examples of how to handle data immutably in JS. There's dozens of immutable update utility libraries available [2]. The one that I'm really excited about right now is a new library called Immer [3], from MobX author Michel Weststrate. It uses ES6 proxies to let you write normal mutative code in a callback, then applies the updates immutably. It looks like it should simplify a lot of common immutable update use cases, like in Redux reducers. [0] https://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html https://redux.js.org/docs/recipes/reducers/ImmutableUpdatePa... [1] https://github.com/markerikson/react-redux-links/blob/master/immutable-data.md https://github.com/markerikson/react-redux-links/blob/master... [2] https://github.com/markerikson/redux-ecosystem-links/blob/master/immutable-data.md#immutable-update-utilities https://github.com/markerikson/redux-ecosystem-links/blob/ma... [3] https://github.com/mweststrate/immer/ https://github.com/mweststrate/immer/
- littlestymaar 9y agoWhat's a nested shallow copy, and how is it different from a deep copy ?
- acemarke 9y agoA shallow copy means creating a new object (or array), where all of the fields or array values point to the same references as the original: const original = {a : {a1 : 42}, b : {b1 : 99} }; const shallowCopy = {...original}; console.log(shallowCopy.a === original.a); // true A deep copy means creating new objects at _every_ level of nesting: const deepCopy = _.cloneDeep(original); console.log(deepCopy.a === original.a); // false To properly do an immutable update for `original.a.a1`, you'd need to copy each level of nesting, and overwrite the fields in that chain: const updated = { ...original, a : { ...original.a, a1 : 123 } }; console.log(original.a === updated.a); // false console.log(original.b === updated.b); // true
- not_kurt_godel 9y agoThe real actual answer is transpile to asm.js from a language that isn't fundamentally a steaming pile of garbage.
- deleted 9y ago[deleted]
- bryanrasmussen 9y ago>If you find yourself deep copying then you're about to level up your JavaScript career. I don't know if I'm supposed to read 'level up your JavaScript career' as you're about to start doing important stuff and get paid for it too, or you're about to increase your technical proficiency by stopping deep copying and finding a better way because it is too painful to keep on (the second interpretation seems to be borne out by the rest of the post, I just wouldn't think of it as leveling up normally) on edit: fixed silly typo
- Cthulhu_ 9y agoDoesn't immutablejs do the deep copying itself? Even better would be a language (could be plain JS) that enforces immutability. How about a new keyword in Typescript?
- littlestymaar 9y agoThere's Object.freeze[1] for that. Of course that's checked only at runtime, because js is a dynamic language … [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jeremyjh 9y agoTypescript has a readonly directive you can declare for interfaces, and it gives a type error on assignment.