Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trunc vs Floor vs ParseInt vs ~~
(version: 0)
Trunc vs Floor vs ParseInt vs ~~
Comparing performance of:
Trunc vs Floor vs ParseInt vs Bitwise invert2
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var num = Math.random() * 100;
Tests:
Trunc
Math.trunc(num);
Floor
Math.floor(num);
ParseInt
parseInt(num);
Bitwise invert2
~~(num);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Trunc
Floor
ParseInt
Bitwise invert2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Trunc
122237208.0 Ops/sec
Floor
116850576.0 Ops/sec
ParseInt
95307352.0 Ops/sec
Bitwise invert2
132347064.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Overview** The benchmark compares the performance of four different mathematical operations: 1. Trunc (short for `Math.trunc()`) 2. Floor 3. ParseInt 4. Bitwise invert (~) These operations are used to truncate or round a given number to the nearest integer. **Trunc (Math.trunc())** `Math.trunc()` is a built-in JavaScript function that truncates a specified number to an integer, rounding down if necessary. It's often used for converting decimal numbers to integers. Pros: * Simple and efficient implementation * Widely supported across different browsers Cons: * Can be slower than other methods, especially for large numbers * May lose precision due to rounding errors **Floor** The `floor()` function returns the largest integer less than or equal to a given number. It's similar to Trunc, but instead of truncating up, it truncates down. Pros: * Fast and efficient implementation * Preserves precision better than Trunc Cons: * May not be suitable for all use cases (e.g., rounding up is needed) **ParseInt** `parseInt()` converts a string or number to an integer. It's more complex than the other two methods, as it needs to parse the input value first. Pros: * Can handle strings and numbers * Preserves precision better than Trunc Cons: * Slower implementation compared to basic arithmetic operations * May throw errors for invalid input values (e.g., non-numeric strings) **Bitwise invert (~)** The bitwise invert operation (~) performs a bit-level operation on the input number. It inverts all bits of the binary representation, effectively performing a two's complement calculation. Pros: * Fast and efficient implementation * Can be useful for specific use cases (e.g., arithmetic operations with integers) Cons: * Difficult to understand and implement due to its bitwise nature * May not preserve precision as well as other methods **DevicePlatform**, **OperatingSystem**, and **Browser** These fields represent the browser, device platform, and operating system used during the benchmarking process. This information helps identify any potential biases or variations in performance. **Alternatives** If you need to perform integer arithmetic operations with JavaScript, consider using: * `Number()` function: returns an integer from a numeric string * `Integer` object (in Node.js): provides a way to perform integer arithmetic However, for simple cases like the ones tested here, basic mathematical operations like Trunc, Floor, and ParseInt are sufficient. For more complex or high-performance applications, consider using specialized libraries or engines. **Other Considerations** Keep in mind that this benchmark only tests specific operations and does not account for other factors that might affect performance, such as: * Input value ranges * Precision requirements * Memory allocation When working with numerical computations in JavaScript, consider these aspects to ensure optimal performance.
Related benchmarks:
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
Math.floor vs Math.trunc
parseInt vs Math.trunc
parseInt vs Math.trunc 2
floor vs trunc vs bit shift
Comments
Confirm delete:
Do you really want to delete benchmark?