Closed Bug 1284590 Opened 8 years ago Closed 8 years ago

Adding numbers is slower if you *don't* use a closure (in the browser only)

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: jorendorff, Assigned: tcampbell)

References

Details

Attachments

(1 file)

Attached file loops.html
I timed these two loops in today's Nightly: // Loop A let s = 0; for (let i = 0; i < a.length; i++) s += a[i]; // Loop B let s = 0; a.forEach(function (v) { s += v; }); Loop B is 5x faster. It's let-related: both loops are fast if you change let to var everywhere. That is not super surprising, but what's weird is that both loops are fast in the shell. Loop A is slow only in the browser.
Oh, shoot. I think I was holding it upside down and it's actually that both loops are slow in the shell. Still doesn't make sense though.
The existence of Loop B affects the performance of Loop A. here's more reduced testcase for shell, there a function expression refers let-variable in the enclosing block. let a = []; for (let i = 0; i < 1000000; i++) a[i] = i; let results = []; for (let n = 0; n < 80; n++) { let s = 0; let t0 = Date.now(); for (let i = 0; i < a.length; i++) s += a[i]; let t1 = Date.now(); results.push(t1 - t0); } { let b = 0; (function () { b; }); } print(results); so far I found 2 cases that performance improves. [case 1] let a = []; for (let i = 0; i < 1000000; i++) a[i] = i; let results = []; for (let n = 0; n < 80; n++) { let s = 0; let t0 = Date.now(); for (let i = 0; i < a.length; i++) s += a[i]; let t1 = Date.now(); results.push(t1 - t0); } { let b = 0; (function () { c; }); // change to other variable than b } print(results); [case 2] let a = []; for (let i = 0; i < 1000000; i++) a[i] = i; let results = []; for (let n = 0; n < 80; n++) { let s = 0; let t0 = Date.now(); for (let i = 0; i < a.length; i++) s += a[i]; let t1 = Date.now(); results.push(t1 - t0); } //{ // remove block let b = 0; (function () { b; }); //} print(results);
(In reply to Tooru Fujisawa [:arai] from comment #2) > The existence of Loop B affects the performance of Loop A. Yup, it's because Loop B has a PUSHBLOCKSCOPE op (since `s` inside the second loop is aliased by the function expression). So the first loop is slower because it runs entirely in Baseline, the second loop is faster because the forEach self-hosted function is Ion-compiled.
Depends on: 1273858
See Also: → 1338910
Assignee: nobody → tcampbell
Will be fixed by Bug 1273858.
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: