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 +
Created:
2 years 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
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:
12 days ago
)
User agent:
Mozilla/5.0 (Linux; Android 16; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.7727.56 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 147 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Number
88.2 Ops/sec
parseInt
109.6 Ops/sec
>>0
114.2 Ops/sec
*1
112.2 Ops/sec
~~
109.9 Ops/sec
+
109.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the ways to convert strings to numbers in JavaScript. **Benchmark Definition** The benchmark definition json provides a basic structure for the test, which includes: * `Name`: The name of the benchmark. * `Description`: A brief description of the benchmark. * `Script Preparation Code`: The code that prepares the data used in the benchmark. In this case, it generates an array of 1 million random integers between 1 and 1000. * `Html Preparation Code`: Not used in this benchmark. **Individual Test Cases** The individual test cases are defined by multiple json objects, each representing a different way to convert strings to numbers: * `Number()`: Uses the built-in `Number()` function to convert strings to numbers. * `parseInt()`: Uses the `parseInt()` function to convert strings to numbers. Note that this will truncate any decimal part of the number. * `>> 0` (bit shift): Uses the bit shift operator (`>>`) to convert a string to an integer by shifting the bits to the right and then casting it back to an integer. * `*1`: Multiplies the string by 1, effectively converting it to a number. This works because in JavaScript, multiplying any value by 0 returns 0, so multiplying by 1 has no effect. * `~~` (integer sign): Uses the bitwise NOT operator (`~`) and then takes the absolute value of the result with `Math.abs()`. However, since this does not cover all edge cases (like `-0`), it is considered slower than `parseInt`. * `+`: Concatenates two strings together and then converts the resulting string to a number using the unary plus operator (`+`). This is a very slow way to convert strings to numbers. **Library Usage** None of these test cases use any libraries. **Special JS Features/Syntax** The following special JavaScript features/syntax are used in some of the test cases: * `>> 0`: A bit shift operator that can be confusing to those not familiar with bitwise operations. * `~~`: The integer sign operator, which is a less common feature and may not be supported by all browsers or environments. **Pros and Cons** Here's a summary of the pros and cons for each approach: * `Number()`, `parseInt()`, and `+` are built-in functions that provide fast and reliable conversions. They have no significant downsides. * `>> 0`: This is a clever way to convert strings to integers, but it relies on bitwise operations, which may be slower than the others. It also assumes that the string can be converted directly to an integer without fractional part. * `*1` is a quick fix, but it doesn't work well for negative numbers or strings containing decimal parts. * `~~`: While this might seem like a good idea, it's not reliable and will fail with certain edge cases. It's generally slower than the other methods. **Other Alternatives** Some alternative methods to convert strings to numbers could be used, such as: * Using a regular expression to extract numerical characters from the string. * Using a library like `json` or `number-expr` that provides functions for parsing and converting strings to numbers. * Using a language-specific function like `python's int()`. However, these alternatives are likely to be slower and less portable than the methods tested in this benchmark.
Related benchmarks:
_orderBy vs javascript arr.sort
Lodash sort vs array.prototype.sort with localeCompare
Lodash max vs JS Math.max (2022)
_.max vs Math.max
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?