Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Is even
(version: 0)
Comparing performance of:
normal vs bitwise
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); }
Tests:
normal
let n = getRandomInt(100) console.log(n % 2 == 0)
bitwise
let n = getRandomInt(100) console.log(n & 1 == 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
normal
bitwise
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the JavaScript microbenchmark on MeasureThat.net. **What is being tested?** The provided benchmark tests two different ways of checking if a number is even: using the modulo operator (`%`) and using bitwise operations with a mask (`&`). In both cases, a random integer `n` is generated between 0 and 100, and then checked if it's even by evaluating an expression. The expressions are: * For the "normal" test case: `let n = getRandomInt(100)console.log(n % 2 == 0)` * For the "bitwise" test case: `let n = getRandomInt(100)console.log(n & 1 == 0)` **Options compared** Two approaches are being compared: 1. **Modulo operator (`%`)**: This is a simple and widely supported way to check if a number is even. It works by finding the remainder of the division of `n` by 2, which will be 0 if `n` is even. 2. **Bitwise operations with a mask (`&`)**: This approach uses bitwise AND operation with a mask value (in this case, `1`) to check if the least significant bit of `n` is set. If it is, then `n` is even. **Pros and Cons** Here are some pros and cons of each approach: * **Modulo operator (`%`)**: + Pros: Simple, widely supported, easy to understand. + Cons: May be slower than bitwise operations for large numbers due to the overhead of division. * **Bitwise operations with a mask (`&`)**: + Pros: Can be faster than modulo operator for large numbers, as it only requires a single bitwise operation. + Cons: May require more memory or CPU cycles for the mask value, and can be less intuitive for beginners. **Library usage** There is no explicit library used in this benchmark. However, some modern browsers may have built-in functions that could potentially optimize these expressions, but they are not used here. **Special JS feature or syntax** There are no special JavaScript features or syntaxes being tested here. The focus is on the basic operations and how different approaches compare performance-wise. **Alternative approaches** Other alternatives to check if a number is even might include: * Using the `Math.floor()` function with a divisor of 2, e.g., `let n = getRandomInt(100)console.log(Math.floor(n / 2) == Math.floor(n / 2))` * Using arithmetic operations like subtraction or addition, e.g., `let n = getRandomInt(100)console.log((n - (n % 2)) == (n - (n % 2)))` However, these approaches are likely to be less efficient and more complex than the modulo operator and bitwise operations. Overall, this benchmark provides a simple and informative way to compare the performance of different ways to check if a number is even in JavaScript.
Related benchmarks:
Labels
Negative precision floor: Lodash vs Math.floor
Fisher-Yates Shuffle
Lodash max vs JS Math.max (2022)
_.max vs Math.max
Comments
Confirm delete:
Do you really want to delete benchmark?