var array = [];
var map = new Map();
var obj = {};
for (var i = 0; i < 20; i++){
array.push(Math.floor(Math.random()*5));
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 = sorted[currentIndex];
if (currentElement < searchElement) {
minIndex = currentIndex + 1;
}
else if (currentElement > searchElement) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return -1;
}
sorted.includes(Math.floor(Math.random()*7))
binaryIndexOf(Math.floor(Math.random()*7))
sorted.indexOf(Math.floor(Math.random()*7))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
binary | |
indexof |
Test name | Executions per second |
---|---|
includes | 21670970.0 Ops/sec |
binary | 24864620.0 Ops/sec |
indexof | 21294478.0 Ops/sec |
Let's break down the provided benchmark definition and options, as well as the alternatives.
Benchmark Definition:
The benchmark measures the performance of three different methods to search for an element in a sorted array:
binaryIndexOf
: A custom binary search function that takes advantage of the fact that the array is already sorted.includes
: The built-in includes
method of the Array
prototype, which uses a linear search algorithm.indexOf
: The built-in indexOf
method of the Array
prototype, which also uses a linear search algorithm.Script Preparation Code:
The script preparation code sets up a small array of 20 random integers between 0 and 5, as well as a sorted version of the array (sorted
). It also defines a custom binary search function called binaryIndexOf
.
Html Preparation Code:
There is no HTML preparation code provided.
Individual Test Cases:
The individual test cases are:
includes
: Searches for a random integer between 0 and 6 in the sorted array using the includes
method.binary
: Searches for a random integer between 0 and 6 in the sorted array using the custom binary search function (binaryIndexOf
).indexof
: Searches for a random integer between 0 and 6 in the sorted array using the built-in indexOf
method.Options Compared:
The benchmark compares three different options:
binaryIndexOf
function uses a binary search algorithm to find the element, which has an average time complexity of O(log n).includes
method uses a linear search algorithm, which has a worst-case time complexity of O(n).indexOf
method also uses a linear search algorithm, with the same worst-case time complexity as the includes
method.Pros and Cons:
minIndex
and maxIndex
variables.includes
method.includes
method.Library/Functionality Used:
binaryIndexOf
) is implemented in JavaScript. It takes advantage of the fact that the array is already sorted to find the element.includes
, indexOf
, and sort
methods are part of the ECMAScript standard, and are implemented in the browser's JavaScript engine.Special JS Features/ Syntax:
None mentioned explicitly in the benchmark definition. However, it's worth noting that the use of bitwise operations (| 0
) in the binaryIndexOf
function is a common technique to perform integer division in JavaScript.
Alternatives:
Other alternatives to implementing custom binary search or using built-in methods like includes
and indexOf
include: