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[] (25 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 < 25; 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:
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 JSON and explain what is tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition:** The benchmark measures the performance of different ways to search for an element in a sorted array: 1. **Includes**: Using the `includes()` method on the sorted array. 2. **Binary search**: Implementing a binary search algorithm within the array itself. 3. **Map.has()**: Using the `has()` method on a Map instance, where each key is an index into the original array. 4. **Obj[]**: Directly accessing an element of an Object by its key (index). **Options Compared:** * Includes: A built-in method for checking if an element is present in an array. * Binary search: An algorithm that finds the position of a target value within a sorted array. * Map.has(): A method for checking if a key exists in a Map instance. In this case, each key corresponds to an index into the original array. * Obj[]: Directly accessing an element of an Object by its key (index). **Pros and Cons:** 1. **Includes**: Pros: * Easy to implement and understand * Built-in method for arrays * Fast, as it only checks if the element is present in the array * Cons: * May not be optimized for performance, especially for large arrays 2. **Binary search**: Pros: * Efficient for finding specific elements within a sorted array * Can be implemented quickly and accurately * Cons: * More complex to implement than `includes()` * Requires the array to be sorted initially, which may not always be the case 3. **Map.has()**: Pros: * Fast and efficient for looking up elements in a Map instance * Can be used with any type of data (not limited to numbers or strings) * Cons: * May require more memory, as it stores each key-value pair in the Map * Not applicable to an array without a corresponding Map instance 4. **Obj[]**: Pros: * Direct access to elements by index can be convenient and fast for simple cases * Cons: * May not be efficient or scalable for large arrays, as it relies on direct indexing **Library Usage:** In this benchmark, the following libraries/libraries-like- constructs are used: * **Array.prototype.includes()**: A built-in method for checking if an element is present in an array. * **Map**: Used to create a Map instance, where each key corresponds to an index into the original array. **Special JavaScript Features/Syntax:** The benchmark utilizes several advanced JavaScript features, including: * **Arrow functions**: Used within `binaryIndexOf()` for concise and readable code * **Template literals**: Not explicitly used in this example, but could be useful in other scenarios Overall, this benchmark provides a comprehensive comparison of different methods for searching an element in a sorted array, allowing users to understand the trade-offs between simplicity, efficiency, and scalability. **Other Alternatives:** * For finding specific elements within a large array, you may want to consider using data structures like Binary Search Trees (BSTs) or Hash Tables. * If working with JSON data, you could also explore methods for efficiently searching and manipulating JSON arrays, such as using the `JSON.parse()` method with the `Array.prototype.indexOf()` function. In summary, this benchmark offers a straightforward comparison of various methods for searching an element in a sorted array, providing valuable insights into performance, efficiency, and scalability considerations.
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[]
Search small array
Include vs Binary search in sorted array vs Map.has() vs Object[] (150 elements)
Comments
Confirm delete:
Do you really want to delete benchmark?