var arr = new Array(15000);
arr.fill({ id: 0 });
arr = arr.map((el, idx) => el.id = idx);
var foo = Math.floor(Math.random() * 15000);
var index = arr.findIndex((num) => num === foo);
var index = -1
for(var i = 0; i < arr.length; i++) {
if(arr[i].id === index) {
index = i;
break;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
findIndex | |
for loop |
Test name | Executions per second |
---|---|
findIndex | 2513.9 Ops/sec |
for loop | 956.6 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is designed to compare two approaches for finding an index in an array: findIndex
(using the Array.prototype.findIndex()
method) versus a traditional for
loop. The test creates a large array of 15,000 elements with id
properties and fills them with random values.
Options Compared
Two options are being compared:
findIndex
: Uses the Array.prototype.findIndex()
method to find the index of the first element that satisfies the provided condition.for
loop: Iterates over the array using a traditional for
loop, checking each element's id
property until it finds a match.Pros and Cons
findIndex
:for
loop:Other Considerations
The benchmark also includes the browser version (Chrome 103), device platform (Desktop), operating system (Windows), and executions per second metrics. This information helps to identify variations in performance across different environments.
Library Used
There is no explicit library mentioned, but Array.prototype.findIndex()
is a part of the JavaScript standard library.
Special JS Feature/Syntax
No special features or syntax are being tested in this benchmark. However, if you're interested in exploring other areas, here are some alternatives:
forEach()
, map()
, filter()
, etc.If you'd like to explore these topics further, feel free to ask!