Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
floor vs trunc
(version: 0)
Comparing performance of:
trunc vs floor
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
trunc
Math.trunc(13.37)
floor
Math.floor(13.37)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
trunc
floor
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/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
trunc
230769728.0 Ops/sec
floor
241738064.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll dive into explaining what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares two functions: `Math.trunc` and `Math.floor`, which are both used to round down a number to its nearest integer. However, `Math.trunc` is a more modern function introduced in ECMAScript 2019 (ES2019), while `Math.floor` has been part of the language since its inception. **Options Compared** The benchmark tests two options: 1. **Math.trunc**: This function uses the IEEE 754 standard to round down a number to its nearest integer. It's more efficient and accurate than `Math.floor` for certain types of numbers. 2. **Math.floor**: This is an older function that rounds down a number by simply discarding the fractional part. **Pros and Cons** * **Math.trunc**: + Pros: More efficient, accurate, and modern approach. + Cons: May not work as expected with some edge cases or very large numbers due to its reliance on IEEE 754 rounding modes. * **Math.floor**: + Pros: Widely supported, simple implementation, and works for most use cases. + Cons: Less efficient and less accurate than `Math.trunc` for certain types of numbers. **Library Usage** The benchmark doesn't explicitly mention any libraries being used. However, it's worth noting that the `Math` object is a built-in library in JavaScript, providing various mathematical functions like `trunc`, `floor`, and others. **Special JS Features or Syntax** This benchmark doesn't use any special JavaScript features or syntax beyond what's standard in modern JavaScript implementations. It's designed to be run in a variety of environments without relying on advanced features. **Alternatives** Other alternatives for rounding down numbers include: * Using the `Math.round` function with a negative second argument (e.g., `Math.round(13.37, -1)`) which would truncate towards zero. * Implementing a custom rounding function using bitwise operations and arithmetic. * Using other libraries or frameworks that provide more advanced rounding functions. Keep in mind that these alternatives might not be as efficient or accurate as the built-in `Math.trunc` function. Overall, this benchmark provides a simple yet informative comparison between two commonly used rounding functions in JavaScript.
Related benchmarks:
Truncating a number to an integer
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
floor vs trunc vs bit shift
trunc vs floor
round vs trunc vs floor vs toFixed vs parseFloat vs parseInt
Comments
Confirm delete:
Do you really want to delete benchmark?