Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
parseInt vs toFixed vs tilde vs bitwise
(version: 0)
Comparing performance of:
parseInt vs toFixed vs Tilde vs Bitwise vs Math.round vs Math.trunc
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="num"></div>
Script Preparation code:
var el = document.getElementById('num');
Tests:
parseInt
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = parseInt(decimal, 10); }
toFixed
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = decimal.toFixed(); }
Tilde
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = ~~decimal; }
Bitwise
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = decimal | 0; }
Math.round
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = Math.round(decimal); }
Math.trunc
var decimal = 30.0; for (var i = 0; i < 10000; i++) { decimal += (i / 1000); el.innerHTML = Math.trunc(decimal); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
parseInt
toFixed
Tilde
Bitwise
Math.round
Math.trunc
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Windows 8
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parseInt
34.9 Ops/sec
toFixed
31.0 Ops/sec
Tilde
31.1 Ops/sec
Bitwise
35.4 Ops/sec
Math.round
35.5 Ops/sec
Math.trunc
35.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **What is being tested?** The benchmark is comparing four different methods to format or manipulate decimal numbers: 1. `parseInt` 2. `toFixed` 3. "Tilde" (which is actually the bitwise NOT operator ~, but it can be used as a shorthand for rounding) 4. Bitwise operation (`decimal | 0`) 5. `Math.round` and `Math.trunc` The test case is creating a decimal number and repeatedly adding a small increment to it, updating an HTML element with the current value using each of these methods. **Options compared** * `parseInt`: converts the decimal number to an integer * `toFixed`: formats the decimal number as a fixed-point string (e.g., "30.0") * "Tilde" (`~~`): uses the bitwise NOT operator to round down to the nearest whole number * Bitwise operation (`decimal | 0`): uses the bitwise AND operator with 0 to truncate the decimal part * `Math.round`: rounds the decimal number to the nearest integer * `Math.trunc`: truncates the decimal number to the nearest whole number (similar to "Tilde" but using a built-in function) **Pros and cons of each approach** * `parseInt`: simple, fast, but may lose precision due to rounding errors * `toFixed`: more precise than `parseInt`, but can be slower due to string formatting overhead * "Tilde" (`~~`): fast, but can produce unexpected results if the input is not an integer * Bitwise operation (`decimal | 0`): very fast, but may be less readable and more prone to errors * `Math.round`: more precise than `parseInt`, but still rounds down * `Math.trunc`: similar to "Tilde" but using a built-in function, which can be faster and more reliable **Other considerations** * The use of bitwise operations is an unusual approach for formatting decimal numbers. While it may be fast, it's not the most readable or maintainable solution. * The use of `~~` as a shorthand for rounding down is also uncommon. It's better to use `Math.trunc` or `Math.round` instead. **Library** None mentioned in this benchmark. However, note that some browsers (like Chrome) have built-in functions like `toFixed` and `Math.round`/`Math.trunc`, which can simplify the implementation. **Special JS feature/syntax** None explicitly mentioned in this benchmark.
Related benchmarks:
parseInt vs toFixed vs ~~
parseInt vs toFixed vs interpolated parseInt vs vs ~~
parseInt() VS x.toFixed()
ParseInt vs conditional ~~ vs toFixed
Comments
Confirm delete:
Do you really want to delete benchmark?