10 ms·
This will work but isn't advised. - Bad: newX = JSON.parse(JSON.stringify(x)) - Slightly Better: newX = lodash.cloneDeep(x) - Best(using immer): newX = produ
by IceHegel 6y ago
This will work but isn't advised.
- Bad: newX = JSON.parse(JSON.stringify(x))
- Slightly Better: newX = lodash.cloneDeep(x)
- Best(using immer): newX = produce(x, draftX => {draftX[1].done = true})
I don't know how JavaScript engines actually assign memory, but I think of object references in JavaScript like pointers in C. If you deep clone you're copying way more than you need, mallocing giant blocks of memory for duplicated data. Libraries like immer(the best) and immutable.js will only copy what you changed, which saves time and memory.