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[] (100 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 < 100; i++){ array.push(Math.floor(Math.random()* 100)); 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
70938480.0 Ops/sec
binary
64030868.0 Ops/sec
Map.has()
166225984.0 Ops/sec
Obj[]
150058848.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of four different approaches to find an element in a sorted array: 1. `includes()` method 2. Binary search (implemented as `binaryIndexOf()` function) 3. `Map.has()` method 4. Direct array access (`obj[5]`) **Script Preparation Code** The script preparation code creates three data structures: * An empty array `array` * A Map `map` with 100 elements, where each element has a unique key and a value of `true`. * An object `obj` with 100 elements, where each element is a property with the same value as in the `map`. The script then sorts the `array` using the `sort()` method. **Individual Test Cases** Each test case measures the performance of one specific approach: 1. **Includes**: Measures the time it takes to find an element (in this case, 5) using the `includes()` method. 2. **Binary**: Measures the time it takes to find an element (in this case, 5) using the custom binary search function. 3. **Map.has()**: Measures the time it takes to check if a key exists in the Map (in this case, the key 5). 4. **Obj[]**: Measures the time it takes to access an element directly in the object (`obj[5]`). **Library and Special JS Features** * `includes()` method: This is a built-in method of JavaScript arrays. * `Map.has()`: This is a built-in method of JavaScript Maps. * Binary search function: Custom implementation, not a standard JavaScript feature. However, this approach demonstrates how to implement binary search in JavaScript. * Direct array access (`obj[5]`): This is a direct property access on an object. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Includes**: Fast and simple, but may be slower than custom implementations for large datasets. 2. **Binary**: More efficient than `includes()` method for large datasets, but requires custom implementation. 3. **Map.has()**: Fast and efficient, especially for large datasets, as it uses a hash table under the hood. 4. **Obj[]**: Direct access is generally fast, but may not be applicable for large objects or complex data structures. **Other Considerations** * The benchmark measures the time it takes to execute each test case, which provides insight into the performance characteristics of each approach. * The use of a sorted array and random keys in the Map and object makes it easier to compare the performance of these approaches. * The benchmark can be useful for identifying potential bottlenecks or optimization opportunities in code. **Alternatives** If you were to optimize this benchmark, you might consider: * Using more efficient data structures, such as a binary search tree or a hash table. * Implementing caching mechanisms to reduce the number of iterations required for large datasets. * Optimizing the custom binary search function to reduce its execution time. * Using other performance optimization techniques, such as parallel processing or lazy loading.
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
Include vs Binary search in sorted array vs Map.has() vs Object[] (150 elements)
Comments
Confirm delete:
Do you really want to delete benchmark?