Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arr.include vs binary search on sorted array vs Map.has() vs Set.has() vs Object[]
(version: 0)
Comparing performance of:
Includes vs binary search vs Map.has() vs Set.has() vs Obj[]
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [] var map = new Map() var set = new Set() var obj = {} for (var i = 0; i < 2000; i++) { arr.push(Math.random()) map.set(i, true) set.add(i) obj[i] = true } arr = arr.sort() function binaryIndexOf(searchElement) { var minIndex = 0 var maxIndex = this.length - 1 var currentIndex var currentElement while (minIndex <= maxIndex) { currentIndex = (minIndex + maxIndex) >>> 1 currentElement = this[currentIndex] if (currentElement < searchElement) { minIndex = currentIndex + 1 } else if (currentElement > searchElement) { maxIndex = currentIndex - 1 } else { return currentIndex } } return -1 }
Tests:
Includes
arr.includes(5)
binary search
binaryIndexOf.call(arr, 5)
Map.has()
map.has(5)
Set.has()
set.has(5)
Obj[]
obj[5]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Includes
binary search
Map.has()
Set.has()
Obj[]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
2176194.2 Ops/sec
binary search
20546908.0 Ops/sec
Map.has()
64704756.0 Ops/sec
Set.has()
69655992.0 Ops/sec
Obj[]
70360144.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is a complex task, and BenchmarkThat.net provides a comprehensive framework for comparing different approaches. The provided benchmark definition tests the following options: 1. **arr.includes(5)**: This method checks if an element (in this case, 5) exists in an array using the "includes" method. 2. **binary search**: This is a custom implementation of a binary search algorithm to find an element in a sorted array. 3. **Map.has(5)**: This method checks if a key (in this case, 5) exists in a Map object. 4. **Set.has(5)**: This method checks if an element (in this case, 5) exists in a Set object. 5. **Obj[5]**: This is an index-based access to an object at index 5. Let's break down each option: **arr.includes(5)**: This method is implemented using the built-in "includes" method in JavaScript arrays. It iterates through the array and checks if the element exists. The time complexity of this method is O(n), where n is the length of the array. Pros: Fast, widely supported, and easy to use. Cons: Linear search can be slow for large datasets. **binary search**: This custom implementation uses a binary search algorithm to find an element in a sorted array. It reduces the search space by half at each step, resulting in a time complexity of O(log n). Pros: Efficient for large datasets, can be faster than "includes" method. Cons: Requires the array to be sorted, which adds overhead. **Map.has(5)**: This method uses the Map object's built-in `has` method to check if a key exists. The time complexity is O(1) on average, making it very efficient. Pros: Fast, widely supported, and easy to use. Cons: May have some overhead due to Map object creation. **Set.has(5)**: This method uses the Set object's built-in `has` method to check if an element exists. The time complexity is O(1) on average, making it very efficient. Pros: Fast, widely supported, and easy to use. Cons: May have some overhead due to Set object creation. **Obj[5]**: This index-based access checks the value at a specific index in an object. The time complexity depends on the object's structure and size. Pros: Fast if the object is small and has a fixed key-value layout. Cons: Slow for large objects or dynamic key-value layouts. The provided benchmark results show that: * Map.has(5) and Set.has(5) are the fastest methods, with average execution times below 300 seconds. * Binary search is faster than "includes" method but slower than Map.has(5) and Set.has(5). * Obj[5] is slow compared to other methods. Other alternatives that can be considered in this benchmark include: * Using `Array.prototype.indexOf()` instead of `arr.includes()` * Using a more efficient binary search algorithm, such as the "exponential search" method * Comparing with other data structures, like Linked Lists or Trees * Adding additional complexity, such as handling edge cases or errors However, these alternatives are not included in this benchmark definition and would require additional modifications to the test code.
Related benchmarks:
Include vs Binary search in sorted array vs Map.has()
Include vs Binary search in sorted array vs Map.has() vs Object[]
Include vs Binary search in sorted array vs Map.has() vs Object[] fork
Include vs Binary search in sorted array vs Map.has() vs Object[] (150 elements)
Comments
Confirm delete:
Do you really want to delete benchmark?