Math.trunc(13.37)
Math.trunc(13.67)
Math.round(13.37)
Math.round(13.67)
Math.floor(13.37)
Math.floor(13.67)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
trunc | |
round | |
floor |
Test name | Executions per second |
---|---|
trunc | 215448992.0 Ops/sec |
round | 229425760.0 Ops/sec |
floor | 211286528.0 Ops/sec |
Let's dive into the explanation of what's tested on the provided JSON and explore the different options being compared.
What is being tested?
The benchmark measures the performance difference between three rounding functions in JavaScript: Math.trunc
, Math.round
, and Math.floor
. Each function is tested with two input values, 13.37
and 13.67
.
Options being compared:
Pros and Cons:
Library and purpose:
The Math
library provides these three rounding functions:
Special JS feature or syntax:
The benchmark doesn't use any special JavaScript features or syntax. It's purely focused on testing the performance difference between these three rounding functions.
Other alternatives:
If you need to round numbers in JavaScript, other alternatives include:
toFixed()
method with a string representation of the decimal places.For example, you can use the toFixed()
method to round numbers:
let number = 13.37;
let roundedNumber = Math.round(number * 100) / 100; // rounds to two decimal places
However, keep in mind that this approach may not be as efficient as using built-in functions like Trunc, Round, or Floor.
In summary, the benchmark measures the performance difference between three rounding functions in JavaScript: Math.trunc
, Math.round
, and Math.floor
. Understanding their differences and trade-offs can help you make informed decisions about which function to use depending on your specific requirements.