parseInt with a string is super slow
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox116 | --- | affected |
People
(Reporter: calixte, Unassigned)
Details
Run this:
function run(nums, cb) {
const t1 = performance.now();
const x = nums.reduce((acc, x) => acc + cb(x), 0);
const t2 = performance.now()
console.log(t2 - t1, x);
}
const nums = [];
for (let i = 0; i < 10000000; i++) {
nums.push(`${Math.floor(Math.random()*1000)}`);
}
run(nums, x => parseInt(x));
run(nums, x => x | 0);
run(nums, x => +x);
On my computer (Windows 11), I get ~1301ms, ~104ms and ~94ms when in Chrome I get ~149ms, ~165ms, ~145ms.
I'd really prefer using parseInt because it's more clear than x | 0.
Comment 1•2 years ago
|
||
Weird, I get numbers like 38, 86, 77 on Mac?
Comment 2•2 years ago
|
||
(In reply to Jan de Mooij [:jandem] from comment #1)
Weird, I get numbers like 38, 86, 77 on Mac?
I got 1153, 121, 115, also on Mac, here's a profile of it: https://share.firefox.dev/3WZBTDB Most of time is in js::LookupName / js::LookupProperty, I don't know what that does in this context.
Comment 3•2 years ago
|
||
Oh I see. You shouldn't run this from the web console, it has known performance issues unfortunately. See bug 793345.
That does explain it though, parseInt requires a slow name lookup in the console environment and the other operations don't.
| Reporter | ||
Comment 4•2 years ago
|
||
I just ran it from a html page and parseInt is finally faster than the two others (37, 105, 96).
Updated•2 years ago
|
Description
•