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)
Core
JavaScript Engine
Tracking
()
RESOLVED
FIXED
People
(Reporter: jorendorff, Assigned: tcampbell)
References
Details
Attachments
(1 file)
1001 bytes,
text/html
|
Details |
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.
Reporter | ||
Comment 1•8 years ago
|
||
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.
Comment 2•8 years ago
|
||
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);
Comment 3•8 years ago
|
||
(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
Assignee | ||
Updated•8 years ago
|
Assignee: nobody → tcampbell
Assignee | ||
Comment 4•8 years ago
|
||
Will be fixed by Bug 1273858.
Assignee | ||
Updated•8 years ago
|
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.
Description
•