Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
bitwise vs compare vs includes
(version: 0)
Comparing performance of:
bitwise vs compare vs includes
Created:
7 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var arr = ['a', 'b', 'c']
Tests:
bitwise
!!~arr.indexOf('c')
compare
arr.indexOf('c') > -1
includes
arr.includes('c')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
bitwise
compare
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
compare
48.4M/s
bitwise
47.9M/s
includes
41.8M/s
View exact numbers
Test name
Executions per second
✓
compare
48,406,576 Ops/sec
bitwise
47,874,012 Ops/sec
includes
41,835,488 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The given benchmark is designed to compare the performance of three different methods for checking if an element exists in an array: bitwise operation (`!!~arr.indexOf('c')`), comparison using `>` (`arr.indexOf('c') > -1`), and includes method (`arr.includes('c')`). The goal is to determine which method is the most efficient. **Test Cases** There are three test cases: 1. **Bitwise**: `!!~arr.indexOf('c')` * This test case uses bitwise operation to check if 'c' exists in the array. * `!!~` is a bitwise NOT operator (~) that flips all bits and then applies two's complement to the result. The `!!` part converts this result back to a boolean value (0 for false, 1 for true). This method is often referred to as "bitwise negation" or "arithmetic negation". * Pros: Can be faster than comparison methods since it's a single operation. * Cons: Less readable and maintainable due to its unconventional syntax. 2. **Comparison**: `arr.indexOf('c') > -1` * This test case uses the traditional comparison method, where `indexOf` returns the index of 'c' if found (or -1 if not), and then the result is compared with -1 using the greater-than operator (`>`). * Pros: More readable and maintainable than bitwise methods. * Cons: Requires a second operation to convert the result to a boolean value, which can be slower than the bitwise method. 3. **Includes**: `arr.includes('c')` * This test case uses the includes method provided by modern JavaScript arrays (introduced in ECMAScript 2015). * Pros: Most readable and maintainable of the three methods, as it clearly conveys its intent. * Cons: May be slower than bitwise methods due to its additional operation. **Library and Special JS Features** There is no explicit library mentioned in the benchmark definition or test cases. However, some features like `!!~` rely on the fact that JavaScript supports bitwise operations and two's complement arithmetic. The includes method relies on the modern array prototype. **Alternatives** Other alternatives for checking if an element exists in an array include: * Using a traditional `for` loop to iterate over the array: `for (var i = 0; i < arr.length; i++) { if (arr[i] === 'c') { ... } }` * Using `in` operator (not commonly used for this purpose): `if ('c' in arr) { ... }` * Using `Array.prototype.some()` method: `arr.some(function(element) { return element === 'c'; })` Keep in mind that these alternatives might be slower and less efficient than the methods tested in the benchmark.
Related benchmarks
bitwise operator vs. boolean logic when using TypedArrays
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
Math.round vs Bitwise
Comments
Confirm delete:
Do you really want to delete benchmark?