Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
(version: 0)
Comparing performance of:
modulus vs bitwise
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var x = Math.random() * 100000000; var y;
Tests:
modulus
y = x % 2;
bitwise
y = x & 1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
modulus
bitwise
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
modulus
1149839104.0 Ops/sec
bitwise
1068376512.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **What is being tested?** The test is designed to compare the performance of three different approaches to achieve the same result: calculating the remainder of an integer division operation: 1. `Math.floor()` 2. `Math.trunc()` 3. Bitwise hacks using `~~` (two's complement), `>> 0`, and other techniques. **Options being compared** The test is comparing the performance of these three approaches for two individual test cases: * "modulus": `y = x % 2;` * "bitwise": `y = x & 1;` In each test case, the script preparation code generates a random integer `x` and then calculates the result using one of the three approaches. **Pros and Cons** Here's a brief analysis of each approach: 1. **Math.floor()**: This is a built-in JavaScript function that returns the largest integer less than or equal to the input value. It's a straightforward and efficient way to calculate remainders, but it may not be optimized for specific platforms. * Pros: Easy to implement, well-supported by browsers. * Cons: May not be the fastest option due to its overhead. 2. **Math.trunc()**: This is another built-in JavaScript function that returns the integer part of a number. It's similar to `floor()` but can return negative values if necessary. * Pros: Similar performance characteristics to `floor()`, and may be faster in some cases. * Cons: May not work as expected for large integers due to its rounding behavior. 3. **Bitwise hacks**: These are low-level optimizations that use bitwise operations to achieve the desired result without using the built-in `Math` functions. * Pros: Can potentially be very fast and efficient, especially on platforms where arithmetic instructions are optimized. * Cons: Require a good understanding of binary arithmetic and may not work across all browsers. **Other considerations** The test also measures the performance of Firefox 83 on Linux desktops. This means that any differences in performance between approaches may be due to platform-specific optimizations or browser-specific behavior. **Library and special JS features** None of the test cases use any external libraries, but they do rely on the following special JavaScript features: * `Math.random()`: a built-in function for generating random numbers. * Arithmetic operators (`%`, `&`): built-in operators that perform integer division and bitwise operations, respectively. **Alternatives** If you want to compare the performance of other approaches or test different scenarios, here are some alternatives to explore: * Use different browsers, such as Chrome, Safari, or Edge, to see if there's a difference in performance. * Test with larger input values to stress the arithmetic capabilities of each approach. * Experiment with different bitwise hack optimizations (e.g., using `x & 0xFF` instead of `x & 1`) to see if you can find faster alternatives. * Investigate other built-in JavaScript functions, such as `Math.abs()` or `Number.isInteger()`, that might be relevant for this type of calculation.
Related benchmarks:
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc)
Math.floor vs bitwise <<
Truncating a number to an integer
floor vs trunc vs bit shift
Comments
Confirm delete:
Do you really want to delete benchmark?