Performance issues with comparing numbers and strings

NEW
Unassigned

Status

()

Core
JavaScript Engine: JIT
P3
normal
4 years ago
7 months ago

People

(Reporter: till, Unassigned)

Tracking

(Depends on: 1 bug, Blocks: 1 bug, {perf})

Trunk
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

4 years ago
Created attachment 821688 [details]
Benchmark showing performance if various isNumeric implementations

The attached benchmark compares various implementations of a test for whether a string is numeric.

Issues uncovered here:
- comparing single characters is slow, as demonstrated in the first isNumeric
- parseInt is comparatively slow
- `parseInt(val) + '' == val` is ~35% faster than `parseInt(val == val` (yes, there are semantic differences. The RHS `val` being coerced to number might explain the perf difference, but it doesn't seem likely.)

Depending on how hard it is to inline parseInt, we could look into self-hosting at least the base10 version of it, with a native slow path.
There is already some room reserved in the String representation to make Int-to-String conversion faster, by just storing an int until the string is flattened. See Bug 654190.
(Reporter)

Comment 2

4 years ago
Ah, nice. Thanks for the heads up.
Depends on: 654190

Updated

4 years ago
Keywords: perf
(Assignee)

Updated

3 years ago
Assignee: general → nobody
Still with us, this is on my MacBook Pro:

Took: 1600
Took: 463
Took: 150
Took: 50

--ion-offthread-compile=off does not materially change anything (everything slows down by a few ms, that's all).

The difference between the second and third is unfortunate, as Till already pointed out.

The difference between the first and the fourth is worrisome, since these are the same program except one using string indexing and one using charCodeAt.  (Cleaning up the first program to cache the value like the fourth does not make much difference.)

Adding a fifth program that uses x.charAt instead of x[n] has the same speed.
Priority: -- → P3
Blocks: 1307062

Updated

7 months ago
Depends on: 1359751
You need to log in before you can comment on or make changes to this bug.