3 ms·
Would like to know this aswell. I know using spread const clone = {...source}; Is faster than object.assign({},source) And seeing as in this case the sourc
by vorticalbox 7y ago
Would like to know this aswell.
I know using spread
const clone = {...source};
Is faster than
object.assign({},source)
And seeing as in this case the source is already a js object it would probably be slow to string and the parse.
- WorldMaker 7y agoKeep in mind that both of those two options are only shallow clones. If you've got any bit of nesting and need a deep clone the code starts to get a lot more complex. In general, in places where I "need" a lot of deep clones I try to use an Immutable library with good reuse/sharing tools, but when you have to deep clone a plain JS object JSON.parse(JSON.stringify(obj)) is incredibly fast in most browsers, despite the fact that you would think string allocation might be a bottle neck.