Closed Bug 1837588 Opened 2 years ago Closed 2 years ago

parseInt with a string is super slow

Categories

(Core :: JavaScript Engine, defect)

Firefox 116
defect

Tracking

()

RESOLVED DUPLICATE of bug 793345
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.

Weird, I get numbers like 38, 86, 77 on Mac?

(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.

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.

I just ran it from a html page and parseInt is finally faster than the two others (37, 105, 96).

Status: NEW → RESOLVED
Closed: 2 years ago
Duplicate of bug: 793345
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.