const x = Math.trunc(12.34 * 10);
const y = Math.trunc(567.89 * 10);
const x = Math.round(12.34 * 10);
const y = Math.round(567.89 * 10);
const x = Math.floor(12.34 * 10);
const y = Math.floor(567.89 * 10);
const x = parseFloat((12.34 * 10).toFixed());
const y = parseFloat((567.89 * 10).toFixed());
const x = ~~(12.34 * 10);
const y = ~~(567.89 * 10);
const x = (0 | 12.34 * 10);
const y = (0 | 567.89 * 10);
const x = parseInt(12.34 * 10);
const y = parseInt(567.89 * 10);
const x = parseInt(12.34 * 10, 10);
const y = parseInt(567.89 * 10, 10);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
trunc | |
round | |
floor | |
parseFloat to int | |
~~ | |
0 | | |
parseInt | |
parseInt, 10 |
Test name | Executions per second |
---|---|
trunc | 7436124.5 Ops/sec |
round | 7450237.0 Ops/sec |
floor | 7451651.5 Ops/sec |
parseFloat to int | 2929326.5 Ops/sec |
~~ | 160367520.0 Ops/sec |
0 | | 154319904.0 Ops/sec |
parseInt | 7348802.5 Ops/sec |
parseInt, 10 | 7398285.0 Ops/sec |
Measuring JavaScript performance is crucial for optimizing code and ensuring it runs efficiently across various browsers and environments.
Let's break down the provided JSON and analyze each test case:
Benchmark Definition
The benchmark compares different methods to convert a floating-point number to an integer, including multiplication by 10. The methods being compared are:
Math.trunc()
Math.round()
Math.floor()
parseFloat()
with toFixed()
for conversion~~
(bitwise NOT)(0 | ...)
(bitwise OR with 0)parseInt()
and parseInt()
with a radix of 10Options Compared
Here's a brief explanation of each method:
Math.floor()
, but avoids rounding errors.with
toFixed()for conversion**: Converts a floating-point number to an integer by extracting the integer part from the
toFixed()` result.(0 | ...)
(bitwise OR with 0): This is another non-standard JavaScript construct. When applied to a number x
, it returns -x
if x
is negative and x
otherwise.Pros and Cons
Here's a brief summary of each method:
Math.trunc()
:Math.round()
: Math.floor()
:Math.trunc()
, fast, and reliable.with
toFixed()`:~~ (bitwise NOT)
:(0 | ...)
(bitwise OR with 0):parseInt()
, but ensures octal numbers are treated correctly.parseInt()
.Results
The benchmark results show that each method performs differently depending on the browser and environment used. However, in general:
Math.trunc()
and Math.floor()
tend to be faster and more reliable than other methods.Math.round()
can lead to rounding errors, especially for very large or small numbers.~~ (bitwise NOT)
is a good alternative to Math.trunc()
, but its non-standard status may cause issues in some environments.(0 | ...)
(bitwise OR with 0) is an interesting approach that can produce unexpected results, especially for negative numbers.Overall, the choice of method depends on the specific requirements and constraints of your project. If you need a simple, fast, and reliable solution, Math.trunc()
or Math.floor()
might be the best option. However, if you're working with strings or want to handle edge cases more robustly, consider using parseInt()
.