Open Bug 1733813 Opened 2 years ago Updated 1 year ago

Replace usages of JSON.parse(JSON.stringify(x)) with structuredClone(x)


(Firefox :: General, task)





(Reporter: mconley, Unassigned)



Bug 1722576 introduced the structuredClone global. According to searchfox, [we've got a few usages of JSON.parse(JSON.stringify(x)) in the tree]( We might be able to replace them with using structuredClone(x) instead.

If so, then we might want to also introduce an ESLint rule to sniff out and discourage future usage of JSON.parse(JSON.stringify(x)).

I think this is a good idea for most "simple" objects. You do have to be careful, because structuredClone and JSON.parse(JSON.stringify(x)) behave differently in some ways.

For example:

JSON.parse(JSON.stringify({x: function() {}})) // { }
structuredClone({x: function() {}}) // Uncaught DOMException: The object could not be cloned.
Assignee: nobody → moz-ian

The Bugbug bot thinks this bug should belong to the 'DevTools::General' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.

Product: Firefox → DevTools

The original SearchFox link is not correct. So here an updated list of entries:

This is not just devtools, as such moving back to Firefox :: General.

Product: DevTools → Firefox

Ian, are you actually working on a patch for this? If not, I'd like to jump in here.


Flags: needinfo?(moz-ian)

Hi Sebo, yes, sorry for the delay. I wrote an eslint rule and its associated tests for this right after taking it, but unfortunately didn't get to finishing the actual replacement (since it seemed like one needed to be careful to check for places where the other behaviour was required, as in comment #1, it wouldn't surprise me if that's used in devtools code somewhere) and it fell to the wayside. Looking back it would have made more sense to just land it with exclusions for the existing uses and then go back and remove them.
I have the patches still and will revive them, but I don't think I'll be able to properly get to it until next week.

Flags: needinfo?(moz-ian)

Sorry, there was a problem with the detection of inactive users. I'm reverting the change.

Assignee: nobody → moz-ian

(In reply to Ian Moody [:Kwan] (UTC+1) from comment #5)

I have the patches still and will revive them

Great to hear that!
(Side note: You can also push WIP patches, so they don't get lost.)

I don't think I'll be able to properly get to it until next week.

No pressure! 😀


The bug assignee is inactive on Bugzilla, so the assignee is being reset.

Assignee: moz-ian → nobody
You need to log in before you can comment on or make changes to this bug.