Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Include vs Binary search in sorted array vs Map.has() vs Object[]
(version: 0)
Comparing performance of:
Includes vs binary vs Map.has() vs Obj[]
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; var map = new Map(); var obj = {}; for (var i = 0; i < 2000; i++){ array.push(Math.random()); map.set(i,true); obj[i]=true; } var sorted = array.sort() function binaryIndexOf(searchElement) { var minIndex = 0; var maxIndex = this.length - 1; var currentIndex; var currentElement; while (minIndex <= maxIndex) { currentIndex = (minIndex + maxIndex) / 2 | 0; currentElement = this[currentIndex]; if (currentElement < searchElement) { minIndex = currentIndex + 1; } else if (currentElement > searchElement) { maxIndex = currentIndex - 1; } else { return currentIndex; } } return -1; }
Tests:
Includes
sorted.includes(5)
binary
binaryIndexOf.call(sorted, 5)
Map.has()
map.has(5);
Obj[]
obj[5]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Includes
binary
Map.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
491243.8 Ops/sec
binary
19182264.0 Ops/sec
Map.has()
70701072.0 Ops/sec
Obj[]
71325104.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested. **Overall Benchmark** The benchmark measures the performance of different ways to check for existence in an array or object: 1. `sorted.includes(5)` (using the `includes()` method) 2. `binaryIndexOf.call(sorted, 5)` (using a custom binary search algorithm) 3. `map.has(5);` (using the `Map.prototype.has()` method) 4. `obj[5]` (direct indexing into an object) **Script Preparation Code** The script preparation code creates: * An empty array (`array`) * A new Map object (`map`) and populates it with 2000 random key-value pairs * An empty object (`obj`) * Sorts the array in ascending order **Library Used: None ( native JavaScript methods)** All four test cases use only built-in JavaScript methods, without importing any external libraries. **Test Cases** Each test case measures the performance of a specific method: 1. `Includes`: Uses the `includes()` method to search for an element in a sorted array. 2. `binary`: Uses a custom binary search algorithm to find an element in a sorted array. 3. `Map.has()`: Uses the `Map.prototype.has()` method to check if a key exists in a Map object. 4. `Obj[]`: Directly indexes into an object using square brackets (`[]`) to access a value. **Pros and Cons** Here's a brief summary of the pros and cons for each approach: 1. **Includes()**: Fast and straightforward, but may not be optimized for large arrays. (High) 2. **Binary Search**: More complex and potentially slower due to the overhead of recursive function calls or iteration. However, it can be more efficient for very large arrays. (Medium-Low) 3. **Map.has()**: May involve additional memory allocations and garbage collection, but is generally fast and efficient. (High) 4. **Obj[]**: Direct indexing into an object is simple but may not be optimized for performance. (Low-Medium) **Other Considerations** * The use of `includes()` can lead to a faster result if the array contains duplicate elements. * Binary search is more suitable for large arrays where random access is needed. **Benchmark Result Interpretation** The provided benchmark results show that: * The execution speed increases as the test case becomes simpler (e.g., `Obj[]` > `binary` > `Map.has()`). * Chrome 127 (a specific browser and version) produces the fastest results for each test case. Keep in mind that these results may not generalize to other browsers or environments.
Related benchmarks:
Include vs Binary search in sorted array
Include vs Binary search in sorted array vs Map.has()
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?