New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace parseInt with Number at get 6% boost #4289
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
What version of Node was this tested on? |
Latest version of Node 18 |
We should have included tests for the inputs we expect. |
I'll open a new pull request with the tests. Thanks for the input @jsumners |
Will need to test floats: Number('1.5') // 1.5
parseInt('1.5', 10) // 1 |
Offtop var { time, timeEnd } = console
var I = 1e8
var y
time("+num")
for (var i = 0; i < I; i++) y = +i
timeEnd("+num")
time("Number")
for (var i = 0; i < I; i++) y = Number(i)
timeEnd("Number")
time("parseInt")
for (var i = 0; i < I; i++) y = parseInt(i)
timeEnd("parseInt") and got the following result
I use node.js v16.14.0 |
For a more accurate comparison, I converted the numbers to strings before processing and got the following result: var { time, timeEnd } = console
var I = 1e8
var y
time("+num")
for (let i = 0; i < I; i++) y = +`${i}`
timeEnd("+num")
time("Number")
for (let i = 0; i < I; i++) y = Number(`${i}`)
timeEnd("Number")
time("parseInt")
for (let i = 0; i < I; i++) y = parseInt(`${i}`, 10)
timeEnd("parseInt")
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Here is a small benchmark for
Number(value)
vsparseInt(value)
Result of micro-benchmark
Number: 67.073ms
parseInt: 206.781ms
Code
Before this pull-request:
After this pull request