const shuffleArray = array => {
const arr = [array];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
var testArr = Array.from({
length: 5000
}, () => Math.floor(Math.random() * 4000));
var scrambled = shuffleArray(testArr);
scrambled.sort((a,b) => {
let idx1, idx2;
for (let i = 0; i < testArr.length;i++) {
if (testArr[i] === a) {
idx1 = i
if (idx2 != null) break
} else if (testArr[i] === b) {
idx2 = i
if (idx1 != null) break
}
}
return idx1 - idx2
});
scrambled.sort((a,b) => testArr.findIndex(t => t === a) - testArr.findIndex(t => t === b));
testArr.map((t) => scrambled.find(s => s === t));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
sort manual idx | |
sort findindex | |
map find |
Test name | Executions per second |
---|---|
sort manual idx | 0.3 Ops/sec |
sort findindex | 58.0 Ops/sec |
map find | 59.9 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark:
Benchmark Definition
The benchmark is defined by the Benchmark Definition
JSON, which provides two scripts: Script Preparation Code
and Html Preparation Code
. The Script Preparation Code
defines a function called shuffleArray
, which shuffles an array of 5000 random numbers. This shuffled array is then stored in the variable scrambled
.
The benchmark itself consists of three test cases:
findIndex
Method: The second test case uses the findIndex
method of the Array prototype, which returns the index of the first occurrence of a specified value.map
Function with find
Method: The third test case uses the map
function to create a new array with the elements in the same order as they appear in the original array, and then uses the find
method to find the corresponding index.Options Compared
The three test cases are compared using different approaches:
findIndex
Method: This approach is more efficient, as it uses a binary search algorithm to find the index of the first occurrence of a specified value.map
Function with find
Method: This approach is even more efficient, as it avoids the need for manual indexing altogether and uses a single pass through the array.Pros and Cons
Here are some pros and cons of each approach:
findIndex
Method:findIndex
method, which may not be supported in older browsers.map
Function with find
Method:Other Considerations
The benchmark also takes into account various device platforms (Desktop, Mobile), operating systems (Mac OS X 10.15.7), and browser versions (Chrome 109).
In terms of special JavaScript features, none are mentioned in the provided code. However, it's worth noting that some of these approaches may require support for modern JavaScript features or polyfills.
Alternatives
If you're interested in exploring alternative approaches, here are a few options:
Array.prototype.findIndex
with a custom implementation: You could implement your own binary search algorithm using findIndex
and compare it to the existing benchmark.Array.prototype.forEach
with manual indexing: You could use the forEach
method instead of map
and manually update an index variable during iteration.Keep in mind that these alternatives may have varying levels of efficiency and complexity compared to the existing benchmark.