Did you know, there's now a native way in JavaScript to do deep copies of objects?
That's right, this structuredClone function is built into the J...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Good to know!
The whole support for workers is narrower: mdn: structuredClone
Sixteen years in JavaScript and I never knew this existed! Nice.
So, you mentioned that JSON.parse(JSON.stringify()) is surprisingly performant; and it is on most web browsers. But on single or dual core CPUs, which are common in embedded devices like smart TVs, set-top boxes, kiosks, and gaming consoles, the JSON parsing and stringification blocks the runtime enough that it can dramatically hit performance.
There's a polyfill at just 1K (min+gzip) so this is quite a bit smaller than underscore, just note the limitations listed in the README.
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍
super
Thanks for sharing this. I got to know something completely new and it seems very powerful also.
Perhaps you could have mentioned also where it does not work - if object contains function or on DOM elements. It also does not work on instances and does work differently on instances of native js objects. I've never used it that way and never looked it up, but here is some food for thoughts.
Great feedback - just updated the article with a new section on this, thanks!
I already saw this before and wondered where the exact difference is. Really helpful post!
Good effort, thank you so very much for this effort.
Awesome, alleviates a lot of effort into deep cloning.
Cheers for the post 🚀🚀🚀
would just like to supply with similar post to link them together - Object Equality
Great Summary, Steve!
Do you know if there is a Node.js equivalent for this ?
Although, there are cases when structuredClone fails.
For instance, when an object has a function property:
// error
structuredClone({
f: function() {}
});
Function properties aren’t supported.
Yep, functions are not serializable