Closed Bug 977009 Opened 11 years ago Closed 11 years ago

Use Cu.cloneInto() instead of JSON.parse(JSON.stringify(obj))

Categories

(DevTools :: General, defect)

defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED
Firefox 32

People

(Reporter: ttaubert, Assigned: Gijs)

Details

(Whiteboard: p=1 s=it-32c-31a-30b.3 [qa-])

Attachments

(1 file)

JSON.parse(JSON.stringify()) is slow. We should rather use Cu.cloneInto() when we want to clone an object. I found several occurrences in devtools code: browser/devtools/framework/connect/connect.js:87: let globals = JSON.parse(JSON.stringify(aResponse)); browser/devtools/profiler/cleopatra/js/parserWorker.js:704: gProfiles[profileID] = JSON.parse(JSON.stringify({ browser/devtools/shared/Parser.jsm:428: let loc = JSON.parse(JSON.stringify(parentLocation)); browser/devtools/shared/Parser.jsm:436: let loc = JSON.parse(JSON.stringify(parentLocation)); browser/devtools/shared/Parser.jsm:444: let loc = JSON.parse(JSON.stringify(parentLocation)); browser/devtools/shared/Parser.jsm:452: let loc = JSON.parse(JSON.stringify(parentLocation)); browser/devtools/shared/Parser.jsm:462: let loc = JSON.parse(JSON.stringify(nodeLocation)); browser/devtools/webconsole/hudservice.js:194: let globals = JSON.parse(JSON.stringify(aResponse)); If you want to clone |obj| into a new object you can do: let newObj = Cu.cloneInto(obj, {});
But wait, there is more: toolkit/devtools/server/actors/profiler.js:174: let subj = JSON.parse(JSON.stringify(aSubject, cycleBreaker)); toolkit/devtools/server/actors/profiler.js:175: let data = JSON.parse(JSON.stringify(aData, cycleBreaker)); toolkit/devtools/server/actors/root.js:354: return JSON.parse(JSON.stringify(aRequest)); toolkit/devtools/server/protocol.js:608: return JSON.parse(JSON.stringify(this.template, function(key, value) {
Nice! <3
I haven't touched all of these cases, because there's some that use a second argument to JSON.stringify to filter output, and cloneInto can't do that. Try push: https://tbpl.mozilla.org/?tree=Try&rev=9fa3dea02817
Attachment #8428663 - Flags: review?(vporof)
Assignee: nobody → gijskruitbosch+bugs
Status: NEW → ASSIGNED
Comment on attachment 8428663 [details] [diff] [review] switch to Cu.cloneInto instead of JSON.parse(JSON.stringify(foo)) because it's faster, Review of attachment 8428663 [details] [diff] [review]: ----------------------------------------------------------------- Assuming all tests still pass.
Attachment #8428663 - Flags: review?(vporof) → review+
Green apart from an intermittent, which I've filed as bug 1016024 remote: https://hg.mozilla.org/integration/fx-team/rev/de783743ebcd
Whiteboard: [fixed-in-fx-team]
Sigh. Except xpcshell tests (which I didn't think to include in my trypushes) went orange, because Cu is not defined in the server root actor: remote: https://hg.mozilla.org/integration/fx-team/rev/fa3cb3c36c30
Pretty sure this is going to break Eddy's work of making the server run without chrome code so that it can be in a worker.
Flags: needinfo?(ejpbruel)
(In reply to Nick Fitzgerald [:fitzgen] from comment #7) > Pretty sure this is going to break Eddy's work of making the server run > without chrome code so that it can be in a worker. Yes, this will break the worker debugger, where Cu is not available. We might be able to come up with a replacement API for Cu.cloneInto in a worker, but we should hold off on this until worker debugging lands.
(In reply to Eddy Bruel [:ejpbruel] from comment #8) > (In reply to Nick Fitzgerald [:fitzgen] from comment #7) > > Pretty sure this is going to break Eddy's work of making the server run > > without chrome code so that it can be in a worker. > > Yes, this will break the worker debugger, where Cu is not available. We > might be able to come up with a replacement API for Cu.cloneInto in a > worker, but we should hold off on this until worker debugging lands. I'm not clear on what you mean here. Do you mean this patch needs to be backed out?
(In reply to :Gijs Kruitbosch from comment #9) > (In reply to Eddy Bruel [:ejpbruel] from comment #8) > > (In reply to Nick Fitzgerald [:fitzgen] from comment #7) > > > Pretty sure this is going to break Eddy's work of making the server run > > > without chrome code so that it can be in a worker. > > > > Yes, this will break the worker debugger, where Cu is not available. We > > might be able to come up with a replacement API for Cu.cloneInto in a > > worker, but we should hold off on this until worker debugging lands. > > I'm not clear on what you mean here. Do you mean this patch needs to be > backed out? Actually, we should be able to do is something like: if (Cu.cloneInto) Cu.cloneInto(...) else JSON.parse(JSON.stringify) to make it work inside a worker, in which case this is fine, and you don't have to do anything. But please, in the future, notify me of these things *before* you land them!
Flags: needinfo?(ejpbruel)
Status: ASSIGNED → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Whiteboard: [fixed-in-fx-team]
Target Milestone: --- → Firefox 32
Marco, can you add this to this iteration?
Status: RESOLVED → VERIFIED
Whiteboard: p=1 [qa-]
(In reply to :Gijs Kruitbosch from comment #12) > Marco, can you add this to this iteration?
Flags: needinfo?(mmucci)
Added to Iteration 32.3
Flags: needinfo?(mmucci)
Whiteboard: p=1 [qa-] → p=1 s=it-32c-31a-30b.3 [qa-]
Product: Firefox → DevTools
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: