Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
round vs trunc vs floor
(version: 0)
Comparing performance of:
trunc vs round vs floor
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
trunc
Math.trunc(13.37) Math.trunc(13.67)
round
Math.round(13.37) Math.round(13.67)
floor
Math.floor(13.37) Math.floor(13.67)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
trunc
round
floor
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
21 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
trunc
551447616.0 Ops/sec
round
567068864.0 Ops/sec
floor
545512640.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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:** 1. **Trunc (Toward Zero)**: Rounds a number to the nearest integer, towards zero. 2. **Round**: Rounds a number to the nearest integer, using the standard rounding mode (up in case of decimal 0.5). 3. **Floor**: Rounds a number down to the nearest integer. **Pros and Cons:** * **Trunc**: Pros - generally faster than Round and Floor due to fewer arithmetic operations. Cons - rounds towards zero, which may not be suitable for all use cases. * **Round**: Pros - provides more accurate results when rounding 0.5 decimal places. Cons - generally slower than Trunc and Floor due to additional arithmetic operations. * **Floor**: Pros - rounds down to the nearest integer, ensuring a smaller result. Cons - may not be suitable for all use cases where rounding up is desired. **Library and purpose:** The `Math` library provides these three rounding functions: * **Math.trunc()**: Returns the largest (for positive numbers) or smallest (for negative numbers) integer less than or equal to a given number. * **Math.round()**: Returns the closest integer to a given number, either rounding up or down depending on the value of the fractional part of that number. * **Math.floor()**: Returns the largest integer not greater than a given number. **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: * Using the `toFixed()` method with a string representation of the decimal places. * Implementing custom rounding logic using bitwise operations or arithmetic expressions. * Utilizing third-party libraries that offer optimized rounding algorithms. For example, you can use the `toFixed()` method to round numbers: ```javascript 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.
Related benchmarks:
toFixed vs toPrecision vs Math.round() vs Math.floorfaster test
Truncating a number to an integer
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
floor vs trunc vs bit shift
round vs trunc vs floor vs toFixed vs parseFloat vs parseInt
Comments
Confirm delete:
Do you really want to delete benchmark?