Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String to number conversion in Vanilla JS
(version: 0)
Comparing performance of:
+ vs Number vs parseInt vs parseFloat
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var toTest = [undefined, null, '-1','-1.1', '1', '100000', 'aaaa'];
Tests:
+
toTest.map((item) => (+item));
Number
toTest.map(Number);
parseInt
toTest.map(parseInt);
parseFloat
toTest.map(parseFloat);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
+
Number
parseInt
parseFloat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
+
3316661.5 Ops/sec
Number
3460362.5 Ops/sec
parseInt
2501251.0 Ops/sec
parseFloat
2898870.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches for converting strings to numbers in JavaScript. The script preparation code defines an array `toTest` containing various string values, including `undefined`, `null`, negative integers, positive integers, and a large integer. **Test Cases** There are four test cases: 1. **"()"**: This approach uses the unary plus operator (`+`) to convert each string in the array to a number. The expression `(+item)` is equivalent to `Number(item)`, which attempts to parse the string as an integer. 2. **"Number()"**: This approach uses the built-in `Number()` function to perform the conversion. 3. **"parseInt()"**: This approach uses the `parseInt()` function, which is similar to `Number()`, but with some differences in behavior (e.g., it can handle prefixes). 4. **"parseFloat()"**: This approach uses the `parseFloat()` function, which attempts to parse a string as a floating-point number. **Pros and Cons** Here's a brief summary of each approach: * **"()" (unary plus)**: + Pros: Simple, widely supported, and works well for integers. + Cons: May not work correctly with non-integer values, such as `NaN` or very large numbers. * **"Number()"**: + Pros: Robust and accurate for most use cases. + Cons: May be slower than other approaches due to the overhead of a function call. * **"parseInt()"**: + Pros: Fast and efficient for integers, but can be less accurate with non-integer values. + Cons: Behavior may vary depending on the radix (prefix) used, which can lead to unexpected results. * **"parseFloat()"**: + Pros: Accurate for floating-point numbers, but slower than other approaches due to the overhead of a function call. + Cons: May not work correctly with very large or very small numbers. **Libraries and Special JS Features** None of the test cases rely on specific libraries. However, it's worth noting that the `parseInt()` function is implemented in JavaScript, but its behavior can be influenced by the surrounding code, such as radix prefixes (e.g., `0x` or `0b`). The `parseFloat()` function, on the other hand, is a built-in JavaScript function. **Other Alternatives** If you were to implement a benchmarking framework for string-to-number conversions in JavaScript, some alternative approaches might include: * Using `BigInt()` and its methods (e.g., `BigInt.parseInt()`) for very large integers. * Implementing a custom conversion function using bitwise operations or arithmetic manipulations. * Testing the performance of different parser libraries or engines, such as ICU or the SpiderMonkey engine. Keep in mind that these alternatives would require significant changes to the benchmark and test cases, so it's essential to consider the scope and requirements of your project before exploring alternative approaches.
Related benchmarks:
Parse string to number
ParseInt vs Number conversion
Convert string to number
Convert String to Number parseInt vs +
Comments
Confirm delete:
Do you really want to delete benchmark?