3 ms·
You can make an interesting challenge out any one of the bigger (in LOC) functions of underscore.js or any promise library. You get a profound "a-ha" moment the
by madflame991 11y ago
You can make an interesting challenge out any one of the bigger (in LOC) functions of underscore.js or any promise library. You get a profound "a-ha" moment the first time you implement `deepClone` or `Promise.all`
- duaneb 11y agoI'm not super familiar with javascript, but are all copies shallow by default? Deep copies are trivial enough to bake into the language.
- rmrfrmrf 11y agoJavaScript objects are always pass reference by value (i.e. you're passing around addresses copied by value which are then dereferenced when needed). Copying objects at all in JavaScript is therefore somewhat non-trivial, especially if you need good performance. Typically, the most performant way to deep-copy an object with an unknown structure is to do: var objCopy = JSON.parse(JSON.stringify(obj)); But that 1) is a rather disgusting hack and 2) can potentially break on certain native JS Objects (like Date, for example). The most straightforward way would be to use a for-in loop with a guard for Object.hasOwnProperty(), but in practice, that's very slow by comparison.
- madflame991 11y ago...and it gets even more interesting if you throw in a few circular references in there
- kolodny 11y agohttps://github.com/kolodny/jsan https://github.com/kolodny/jsan ;)