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[] (150 elements)
(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 < 150; 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):
I'll break down the provided benchmark definitions and explanations for each test case. **Benchmark Definition JSON** The main benchmark definition is a JSON object that contains information about the benchmark, such as its name, description, script preparation code, and HTML preparation code (which is empty in this case). The individual test cases are listed in an array. Each test case represents a different approach to searching for a value within a sorted array. **Individual Test Cases** ### 1. `sorted.includes(5)` - "Includes" This test case uses the `includes()` method of the Array prototype to search for the value 5 within the sorted array. Pros: * Simple and concise implementation. * Efficient, as it only iterates over the elements in the array that are less than or equal to the target value. Cons: * May be slower than other approaches if the array is very large, as it needs to iterate over all elements before finding the first one that meets the condition. ### 2. `binaryIndexOf.call(sorted, 5)` - "Binary" This test case uses a custom binary search algorithm implemented in JavaScript. It takes advantage of the fact that the array is already sorted. Pros: * Can be very fast for large arrays, as it only needs to iterate over half of the elements at each step. * Avoids the overhead of method calls and potential caching issues associated with `includes()`. Cons: * Requires manual implementation of the binary search algorithm, which can be error-prone and difficult to maintain. ### 3. `map.has(5);` - "Map.has()" This test case uses the `has()` method of a Map object to search for the value 5 within the map. Pros: * Can be very fast, as Map lookups are typically O(1) operations. * Allows for efficient insertion and deletion of elements from the map. Cons: * Requires an additional data structure (the map), which may add overhead for large arrays. ### 4. `obj[5]` - "Obj[]" This test case uses direct array indexing to search for the value 5 within the sorted array. Pros: * Simple and concise implementation. * Avoids the overhead of method calls associated with other approaches. Cons: * Can be slower than other approaches, as it needs to iterate over all elements in the array before finding the first one that meets the condition. **Library Used** The `includes()` method uses a built-in Array prototype method implemented by the JavaScript engine. The binary search algorithm used in this test case is also a standard approach. **Special JS Feature or Syntax** None of these approaches use any special JavaScript features or syntax, other than the `let` and `const` keywords for variable declarations (not shown in the provided code). **Other Alternatives** Other alternatives to search for values within an array include: * Using a data structure like a Set, which provides fast lookup and insertion operations. * Implementing a linear search algorithm that iterates over all elements in the array. * Using a library or framework that provides optimized search functionality, such as the `binarySearch()` method in some collections. Keep in mind that these alternatives may have different trade-offs in terms of performance, memory usage, and code complexity.
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[] fork
Comments
Confirm delete:
Do you really want to delete benchmark?