5 ms·
This posts advice isn’t good because structuredClone performance is poor. Theres a small custom function I use for this which has much better performance. If y
by user3939382 3y ago
This posts advice isn’t good because structuredClone performance is poor. Theres a small custom function I use for this which has much better performance.
If you’re so inclined you can google the benchmarks.
- KTibow 3y agoWhat types of objects are you copying?
- binarymax 3y agoThe article states differently, saying it is performant. Instead of saying we should search for benchmarks why not post them here? Also, what is the small function you use?
- hutzlibu 3y agoSadly I can confirm, structuredClone is still not performant. At least on my various devices. I just did a test again with 40000 times cloning a simple object and JSON.parse/stringify was 6 times faster than structured cloning. Since I heavily use deep cloning, I am still disappointed with the new native, but slower solution. (And was hoping there would be more than old news in the article) Edit: Code for testing was as simple as for ... 40000 JSON.parse(JSON.stringify(obj)) vs structuredClone(obj)
- meepmorp 3y ago> I just did a test again with 40000 times cloning a simple object and JSON.parse/stringify was 6 times faster than structured cloning. Depending on the object you're cloning, that might be fine. But some types, like Date or Set, don't round trip between JS and JSON so you'd have mangled objects.
- hutzlibu 3y agoYes I know, but 6 times slower is pretty bad, so when someone presents the modern way, he should probably include a warning, that modern not necesarily means better. Because those who used the old way before, know the limits and worked around them. So why should I switch? I wasted time trying it out (not now, when structuredClone came out). Because my assumption was, it should be faster than the JSON abuse. But it is not, it just has more features.
- hutzlibu 3y ago"Theres a small custom function I use for this which has much better performance." Can you enlighten us? JSON.parse and stringify, or customly copying what is needed? (the fastest solution to my knowledge)
- user3939382 3y agoMy own version is proprietary so I can't release it but here's a reference and something like it: https://www.measurethat.net/Benchmarks/Show/17150/0/lodash-clonedeep-vs-structuredclone-vs-recursivedeepcop https://www.measurethat.net/Benchmarks/Show/17150/0/lodash-c...