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[] fork
(version: 0)
Comparing performance of:
Includes vs binary vs Map.has() vs Obj[]
Created:
2 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 < 200; 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:
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to search for an element in a sorted array: 1. `includes()` method 2. Binary search using `binaryIndexOf` function 3. `has()` method on a Map object 4. Array indexing (`obj[5]`) on an Object array **Options being compared** The benchmark compares the performance of each approach on the same input data: a sorted array with 200 random elements. **Pros and Cons of each approach** 1. **`includes()` method**: * Pros: Easy to use, built-in function, works for arrays. * Cons: May have a higher overhead due to the function call and potential bounds checks. 2. **Binary search using `binaryIndexOf` function**: * Pros: More efficient for large datasets, can be implemented in native code for better performance. * Cons: Requires manual implementation of binary search algorithm, may not work as expected for non-sorted arrays. 3. **`has()` method on a Map object**: * Pros: Fast lookup, works well for maps and other hash-based data structures. * Cons: May require additional memory allocation and object creation. **Library usage** The `binaryIndexOf` function uses the JavaScript built-in `Array.prototype.indexOf()` method under the hood. However, in this benchmark, it is implemented manually to compare its performance with the built-in implementation. **Special JS feature or syntax** None of the tested approaches rely on any special JavaScript features or syntax. **Other considerations** * The benchmark assumes that the input array is sorted. If the array were not sorted, the binary search approach would not work correctly. * The `binaryIndexOf` function implementation uses a manual binary search algorithm, which may not be as efficient as the built-in `Array.prototype.indexOf()` method for very large datasets. **Alternatives** Other alternatives to compare the performance of these approaches include: 1. Using `Array.prototype.lastIndexOf()` instead of `includes()` 2. Implementing a linear search algorithm (e.g., using `for` loop) instead of binary search 3. Comparing the performance of different sorting algorithms for the input array (e.g., bubble sort, quicksort) 4. Testing other data structures, such as Linked Lists or Trees
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[]
Include vs Binary search in sorted array vs Map.has() vs Object[] (150 elements)
Comments
Confirm delete:
Do you really want to delete benchmark?