DEV Community

Cover image for Copy Objects Ways in JavaScript

Copy Objects Ways in JavaScript

Fatemeh Paghar on March 16, 2024

To copy objects in JavaScript, you typically have three options: using the assignment operator (=) for reference copying, performing a shallow copy...
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

You missed structuredClone.

Collapse
 
jmpp profile image
J.M. Cléry • Edited

⚠️ Moreover JSON.parse and stringify are very bad for performance. (Incorrect. See discussion below)

Totally agree with the first comments that structuredClone should be the only way in modern codebases to deep clone objects. If you need alternatives, you can use libraries for that (e.g. _.cloneDeep())

Collapse
 
htho profile image
Hauke T.

Performance of JSON.parse(JSON.stringify(obj)) vs. structuredClone(obj) depends on the kind of data you want to clone.

While for more (almost) shallow data the JSON approach is faster, for deeply nested data structuredClone(obj) becomes more performant:

measurethat.net/Benchmarks/Show/30...

The test is very deeply nested, but plausible.

As annoying as this is - there is no silver bullet (performance-wise).
You need to know what kind of data your application will handle, you need to test your solution based on your knowledge about the target audience and their hardware.

Thread Thread
 
mamohsen profile image
Mahmoud Abdel Mohsen

I guess you are right @htho , structuredClone(obj) seems faster in almost all cases unless the data was utterly nested (an edge case).

However, for edge cases I wouldn't use JSON.parse(JSON.stringify(obj)) either. I'd rather stay with structuredClone(obj) for a safer copy option.

jsbm 100 copies
jsbm 1 copy

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Are you sure? Last time I checked structuredClone was dramatically slower than parse and stringify.

measurethat.net/Benchmarks/Show/18...

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Yup, structuredClone does seem much slower.

Collapse
 
jangelodev profile image
João Angelo

Hi Fatemeh Paghar,
Your tips are very useful
Thanks for sharing

Collapse
 
mynameisnextstep profile image
Islam • Edited

The overall pros of JSON.parse performance wise is that the runtime ready to deal with that better since the JSON like structure is a well-suitable and "recognizable" for the runtime and underlying mechanisms of working with it are nicely optimized.

Collapse
 
xuho profile image
Nguyen Xuan hoa

With nested object, we can use recursion and Object.assign() or Spread Operator to make deep copy too :)

Collapse
 
starcradle101 profile image
Hoon Kang

Thank you for all the posts and discussions! While I was already familiar with shallow copying an object, I hadn't known about creating deep copies until now. I've learned several approaches, along with their pros and cons.
Your post is a lot helpful! 👍🏻

Collapse
 
adya_kalhari profile image
Adya Kalhari

This is a great article on the different ways to copy objects in JavaScript! I particularly liked the explanation of reference copies, shallow copies, and deep copies. I also found the comparison of JSON.parse() and JSON.stringify() with structuredClone() to be very informative.

Here's a link to my blog post on object copying in JavaScript, which goes into more detail here.

I hope this is helpful!

Collapse
 
nicholasbalette profile image
Nicholasbalette

Refreshing