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 = arr.indexOf(foo);
var index = arr.includes(foo);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
findIndex | |
indexOf | |
includes |
Test name | Executions per second |
---|---|
findIndex | 24973.3 Ops/sec |
indexOf | 520703.7 Ops/sec |
includes | 519228.3 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark that compares the performance of three different methods: findIndex
, indexOf
, and includes
(all part of the Array prototype) when searching for an element in an array.
Test Cases
The test cases are:
findIndex
: Finds the index of the first occurrence of the specified value in the array.indexOf
: Finds the index of the first occurrence of the specified value in the array, returning -1 if not found.includes
: Returns a boolean indicating whether an element with the specified value exists in the array.Options Compared
The benchmark compares three different approaches to achieve the same goal:
findIndex
: This method is generally faster than indexOf
because it stops iterating as soon as it finds the matching element, whereas indexOf
continues searching even after finding a match.indexOf
: As mentioned earlier, this method is slower than findIndex
, but still relatively fast. It's often used when you want to know if an element exists in the array, regardless of its index.Pros and Cons
Here are some pros and cons of each approach:
findIndex
:indexOf
:findIndex
, but still relatively fast.Library and Syntax Considerations
In this benchmark, no special libraries or syntax are used. However, it's worth noting that some browsers may have different implementations of these methods, which could affect performance.
Other Alternatives
If you need to search for an element in an array and don't care about its index, includes
might be a suitable alternative to indexOf
. If you need the index or want more control over the search process, you can use a simple loop or findIndex
.
In summary, findIndex
is generally faster than indexOf
, but may not be suitable for all use cases. indexOf
provides a convenient way to check if an element exists in the array, while includes
offers a concise alternative when you just need to know if an element is present.
Benchmark Result Interpretation
The latest benchmark result shows that:
findIndex
performs the best on this specific test case.indexOf
follows closely behind, with a slightly lower execution rate than findIndex
.includes
has the lowest execution rate among the three options.These results suggest that findIndex
is likely to be the fastest option for this specific use case. However, it's essential to keep in mind that benchmark results can vary depending on the specific browser and hardware being used.