Replace JSON.parse(JSON.stringify(obj)) with structuredClone

🛑 Stop using JSON.parse(JSON.stringify(obj)) to deep copy objects It’s the first answer on StackOverflow. Most of us have used it at least once. And it still shows up in production code more often than it should. const copy = JSON.parse(JSON.stringify(original)); This worked back when we didn’t really have a better option. But if this is still in your codebase , it’s probably breaking data in way you don’t notice immediately Why this approach is risky 1.Dates don’t survive new Date() quietly turns into a string. 2.Some values just disappear undefined and functions are removed without any warning. 3.Circular references crash the app One self-reference and everything blows up. None of this fails loudly. That’s the scary part. A better option today JavaScript now has a native way to handle this properly. const original = {  date: new Date(),  social: undefined }; const copy = structuredClone(original); Why this works better 1.Dates stay as Dates 2.undefined stays intact 3.Maps, Sets, and circular references are handled correctly 4.Supported in modern browsers and Node.js No hacks. No surprises. The web changes fast. Things that felt “clever” a few years ago don’t always age well. If you still have JSON.parse(JSON.stringify(...)) sitting in a helper file somewhere, it might be time to clean it up. Small change. Fewer bugs. #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #CleanCode #ReactJS #NodeJS #TechTips #ProgrammingHumor #FrontendDevelopment

  • graphical user interface, text, website

Fair point on structuredClone(). Though it too has limits – functions and prototype chains don’t survive the clone. The more interesting question: How often is deep cloning a workaround for mutable state that ought to be immutable by design?

agreed. but you still options avaliable in loadash. which is supporting from at least from last 7 years.

Like
Reply

You can just spread in a new object as well

Like
Reply

What if we use... const newObj={ ...objectToCopy }

This sent me down a rabbit hole. Im just getting deeper into full stack and making my own APIs for database management and I had no idea I was making shallow clones with newObj = {...oldObj} I went and read up on the topic and im glad I did, because I was working on a system that would have been affected by this. It sounds like structuredClone() is a good solution for me but I had no clue how many variables there were to consider here

Like
Reply

Another option is to spread the parts you need into a new const object with the `...` syntax. This copies each prop separately and you can choose to keep or replace anything during your copy.

Like
Reply

Thank you for this post, I have recently started learning the js and node, so I use JSON.parse to practice a lot, now I am going to use the way which you mentioned, thanks again

Like
Reply

I am a bit far from hardcore Javascript world, but... you guys really used in production something like this JSON hack? It even looks buggy, lol)

functions arent meant to be parsed by json also.

Well explained ,thanks for sharing :)

See more comments

To view or add a comment, sign in

Explore content categories