DEV Community

Cover image for Deep Cloning Objects in JavaScript, the Modern Way

Deep Cloning Objects in JavaScript, the Modern Way

Steve Sewell on January 18, 2023

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...
Collapse
 
pengeszikra profile image
Peter Vivo

Good to know!

The whole support for workers is narrower: mdn: structuredClone

Image description

Collapse
 
kixxauth profile image
Kris Walker

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.

Collapse
 
mindplay profile image
Rasmus Schultz

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.

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
sahibjadatalib_ansari profile image
Sahibjadatalib Ansari • Edited

super

Collapse
 
rahul_jindal profile image
Rahul Jindal

Thanks for sharing this. I got to know something completely new and it seems very powerful also.

Collapse
 
webduvet profile image
webduvet

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.

d1 = new Date();
d2 = structuredClone(d1);

d1.__proto__ === d2.__proto__
// true

function Test() {}
Test.prototype.fn = function() {}

t1 = new Test()
t2 = structuredClone(t1);

t1.__proto__ === t2.__proto__
// false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
steve8708 profile image
Steve Sewell

Great feedback - just updated the article with a new section on this, thanks!

Collapse
 
ivofehn profile image
IvoFehn

I already saw this before and wondered where the exact difference is. Really helpful post!

Collapse
 
parthprajapati profile image
Parth

Good effort, thank you so very much for this effort.

Collapse
 
remrkabledev profile image
Malcolm R. Kente

Awesome, alleviates a lot of effort into deep cloning.
Cheers for the post 🚀🚀🚀

Collapse
 
frontendengineer profile image
Info Comment hidden by post author - thread only accessible via permalink
Let's Code

would just like to supply with similar post to link them together - Object Equality

Collapse
 
jareechang profile image
Jerry

Great Summary, Steve!

Do you know if there is a Node.js equivalent for this ?

Collapse
 
ruslancik profile image
Ruslan Bairamovi

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.

Collapse
 
oxavibes profile image
Stefano

Yep, functions are not serializable

Some comments have been hidden by the post's author - find out more