Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare the ways to convert String to Number in JS +
(version: 0)
Compare the most popular ways to convert string to number
Comparing performance of:
Number vs parseInt vs >>0 vs *1 vs ~~ vs + vs |
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const minimum = 1 const maximum = 1000 var arr = Array.from({ length: 1000000 }, () => Math.floor(Math.random() * (maximum - minimum + 1)) + minimum)
Tests:
Number
return arr.map((el) => Number(el))
parseInt
return arr.map((el) => parseInt(el))
>>0
return arr.map((el) => el >> 0)
*1
return arr.map((el) => el * 1)
~~
return arr.map((el) => ~~el)
+
return arr.map((el) => +el)
|
return arr.map((el) => el|0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
Number
parseInt
>>0
*1
~~
+
|
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Number
83.1 Ops/sec
parseInt
125.7 Ops/sec
>>0
173.9 Ops/sec
*1
166.0 Ops/sec
~~
166.9 Ops/sec
+
174.0 Ops/sec
|
127.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarking! **Benchmark Definition** The benchmark, "Compare the ways to convert String to Number in JS +", tests various methods for converting strings to numbers in JavaScript. **Script Preparation Code** The script preparation code generates an array `arr` with 1 million random integers between 1 and 1000. This array is used as input for the conversion tests. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only focuses on JavaScript performance. **Individual Test Cases** Each test case compares a different method for converting strings to numbers: 1. **Number**: Directly converts the string using `Number()`. 2. **parseInt**: Uses the `parseInt()` function to convert the string. 3. **>>0**: Shifts the bits of the number in the string, effectively removing leading zeros (only works with integers). 4. ***1**: Converts the string by multiplying it by 1 (no effect on floating-point numbers). 5. **~~**: Uses the bitwise NOT operator (~) to convert the string. 6. **+**: Uses the unary plus operator (+) to convert the string. **Pros and Cons of Each Approach** Here's a brief overview: * **Number**: Direct conversion is often the fastest, but may throw errors if the input is not a valid number. * **parseInt**: Can be faster for integers, but slower for floating-point numbers. It also assumes the radix (base) is 10. * **>>0**: Works only with integers and can remove leading zeros, making it a good choice for formatting numbers. However, it's slower than direct conversion. * ***1**: Converts any number (int or float) to an integer, but has no effect on floating-point numbers. * **~~**: A more efficient alternative to `parseInt`, but can be less intuitive to use. * **+**: Fast and efficient, but only works for integers. Not suitable for floating-point numbers. **Library and Special Features** In this benchmark, the following libraries are used: * None explicitly mentioned, but the test cases assume standard JavaScript functionality. No special JavaScript features are being tested in this benchmark. **Other Alternatives** For converting strings to numbers, other methods might include: * ` parseFloat()` for floating-point conversion * Using a library like `number-forces` or `js-number-format` for more complex formatting and validation * Regular expressions (regex) for string manipulation Keep in mind that the choice of method depends on the specific use case, performance requirements, and desired behavior. **Benchmark Results** The benchmark results show the executions per second for each test case. The top-performing methods are direct conversion (`Number`) and `parseInt`, while the slower methods are those involving bitwise operations or decimal conversions (`>>0`, `*1`, `~~`).
Related benchmarks:
_orderBy vs javascript arr.sort
Lodash max vs JS Math.max (2022)
_.max vs Math.max
sort variants test
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?