Math.trunc(13.37)
Math.trunc(13.67)
Math.round(13.37)
Math.round(13.67)
Math.floor(13.37)
Math.floor(13.67)
parseInt(12.17)
parseInt(12.97)
parseInt(12.17, 10)
parseInt(12.97, 10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
trunc | |
round | |
floor | |
parseInt | |
parseInt Radix |
Test name | Executions per second |
---|---|
trunc | 6874216.5 Ops/sec |
round | 7101818.0 Ops/sec |
floor | 7088877.0 Ops/sec |
parseInt | 7036161.0 Ops/sec |
parseInt Radix | 7029461.0 Ops/sec |
Let's break down the provided benchmark and its test cases to understand what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of different methods for rounding or truncating decimal numbers in JavaScript:
Math.trunc()
: Truncates a number towards zero.Math.round()
: Rounds a number to the nearest integer.Math.floor()
: Rounds a number down to the nearest integer.parseInt()
with and without radix: Parses an integer from a string, optionally specifying the radix (base) of the input.Test Cases
Each test case is defined in the "Benchmark Definition" field as a JavaScript expression that executes two consecutive rounds or truncations. For example:
trunc
: Math.trunc(13.37)
followed by Math.trunc(13.67)
round
: Math.round(13.37)
followed by Math.round(13.67)
floor
: Math.floor(13.37)
followed by Math.floor(13.67)
parseInt
: parseInt(12.17)
followed by parseInt(12.97)
parseInt Radix
: parseInt(12.17, 10)
followed by parseInt(12.97, 10)
Library and Special Features
The benchmark uses the built-in JavaScript functions Math.trunc()
, Math.round()
, Math.floor()
, and parseInt()
.
There are no special features or syntaxes mentioned in the test cases.
Options Compared
The benchmark compares different rounding/truncation methods, specifically:
Math.trunc()
Math.round()
and Math.floor()
parseInt()
with and without specifying the radixPros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Math.trunc()
)Math.round()
and Math.floor()
)parseInt()
with radix)Other Alternatives
If you need to compare more advanced numerical computations or specific edge cases, consider the following alternatives:
Keep in mind that these alternatives might not be as straightforward to use as the built-in functions, but they can provide more precise and robust results for specific use cases.