Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
parseInt vs ~~
(version: 0)
Benchmark.js
Comparing performance of:
parserInt vs ~~
Created:
4 years ago
by:
Guest
Go to the latest result
HTML Preparation code:
<div id=''></div>
Script Preparation code:
var baseStr = '23';
Tests:
parserInt
const a = parseInt(baseStr)
~~
const b = ~~(baseStr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
parserInt
~~
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Browser/OS:
Chrome 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
~~
250.5M/s
parserInt
249.5M/s
View exact numbers
Test name
Executions per second
✓
~~
250,450,320 Ops/sec
parserInt
249,467,744 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided JSON and benchmark. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case, specifically comparing two approaches to convert a string to an integer: `parseInt()` and the bitwise NOT operator (`~~`). **Options Compared** The benchmark compares the execution time of: 1. `parseInt(baseStr)`: Uses the built-in `parseInt()` function to convert the string `baseStr` to an integer. 2. `~~(baseStr)`: Uses the bitwise NOT operator (`~~`) to perform a negative integer arithmetic operation, effectively converting the string to an integer. **Pros and Cons of Each Approach** 1. `parseInt(baseStr)`: * Pros: Widely supported and well-documented, performs integer conversion correctly. * Cons: Can be slower than the bitwise NOT approach due to the overhead of function calls and type conversions. 2. `~~(baseStr)`: * Pros: Faster and more lightweight compared to `parseInt()`, as it leverages bitwise operations. * Cons: May not work correctly for non-numeric input strings, and its behavior can be counterintuitive due to the use of negative integer arithmetic. **Library and Special JS Feature** There is no specific library used in this benchmark. However, note that the `~~` operator relies on a special JavaScript feature called "bitwise NOT with unboxed numeric arguments" (ECMAScript 2015+), which allows the bitwise NOT operator to operate on numbers directly without first converting them to integers. **Benchmark Preparation Code** The script preparation code creates a variable `baseStr` and assigns it the string value `'23'`. **Individual Test Cases** Each test case is defined by a "Benchmark Definition" that specifies how to run the test. In this case, there are two test cases: 1. `"const a = parseInt(baseStr");`: Runs the code snippet inside the `parseInt()` function. 2. `"const b = ~~(baseStr);"`: Runs the code snippet inside the bitwise NOT operator (`~~`) with the string as an argument. **Benchmark Result** The latest benchmark result shows that: * The `~~` approach executed approximately 33 times faster than the `parseInt()` approach, with a raw execution time of around 24544528.0 executions per second. * Both tests were run on Chrome 104 browser on a desktop device with Mac OS X 10.15.7 operating system. **Other Alternatives** If you're interested in exploring other alternatives for integer conversion, consider using: 1. `Number()` function: Similar to `parseInt()`, but can convert strings to floating-point numbers if possible. 2. `atob()` and `decodeURIComponent()` functions: Can be used to convert a string to an unsigned 32-bit integer, but might not provide accurate results for negative integers. For more advanced or performance-critical scenarios, you may want to explore other libraries like Fast.js, which provides optimized integer conversion methods.
Related benchmarks
querySelector vs getElementById
getElementById vs id
parentElement vs parentNode
Comments
Confirm delete:
Do you really want to delete benchmark?