Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Int conversion
(version: 0)
Comparing performance of:
parseInt vs +str vs Number()
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
parseInt
var str = "150"; parseInt(str,10);
+str
var str = "150"; +str;
Number()
var str = "150"; Number(str);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
parseInt
+str
Number()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parseInt
108334600.0 Ops/sec
+str
121556184.0 Ops/sec
Number()
103524528.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is empty, which means that the script preparation code is not provided. This can be either intentional or an omission. In this case, we'll assume it's intentional. **Individual Test Cases** There are three test cases: 1. `parseInt`: Tests the `parseInt` function, which converts a string to its integer equivalent. The benchmark definition specifies that the input string is `"150"` and the radix is set to 10. 2. `+str`: Tests the expression `+str`, where `str` is the input string `"150"`. This is equivalent to concatenating an empty string with the input string using the unary plus operator (`++`) or using the spread operator (`...`). 3. `Number()`: Tests the `Number()` function, which converts a string to its floating-point number equivalent. **Options Compared** In each test case: * For `parseInt`, only one execution is being measured: converting the input string `"150"` to an integer with radix 10. * For `+str` and `Number()`, multiple executions are likely being measured, as these functions can handle different formats of input strings. However, in this specific benchmark, only two executions are shown: * For `+str`, converting the input string `"150"` to a number using the unary plus operator (`++`) or spread operator (`...`). * For `Number()`, no additional execution is explicitly shown, but it's likely that the test measures multiple conversions with different formats of input strings. **Pros and Cons** * **`parseInt`:** * Pros: * Specifics the radix for accurate conversion. * Fastest execution time. * Cons: * Only tested with a single radix value. * May not handle edge cases or invalid input. * `+str` and `Number()`: * These functions are generally faster than `parseInt` due to their ability to parse multiple formats and handle more cases. * However, they might be slower in certain specific scenarios where the radix is known (as with `parseInt`). Additionally, these methods may still not cover all possible cases. **Library Usage** None of the test cases explicitly use a JavaScript library. The focus is on testing the built-in functions and syntax. **Special JS Features or Syntax** There are no special features or syntax mentioned in the benchmark definitions or test results. **Alternatives** For converting strings to numbers, alternatives include: * **`Number()` with `replace()`:` * Convert input string to a number using `Number()` and then apply any necessary formatting with `replace()`. * **Using regex:** * Use regular expressions (regex) to extract the numeric part from the input string. * **Manual parsing:** * Implement manual logic to parse the input string, potentially using techniques like removing non-numeric characters and then converting what remains to a number. Each approach has its pros and cons, depending on the specific requirements and constraints of your application.
Related benchmarks:
bitwise operator vs. boolean logic when using TypedArrays
Number format
Number Conversion Speed
Intl.NumberFormat vs existing way
Formatting number, including NaN
Comments
Confirm delete:
Do you really want to delete benchmark?