var arr = [];
var i = 0;
while (i <= 1E5) arr[i] = i++;
const index = arr.indexOf(1E5);
const included = arr.includes(1E5);
const item = arr.find(item => item == 1E5);
const index = arr.findIndex(item => item == 1E5);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf | |
includes | |
find | |
findIndex |
Test name | Executions per second |
---|---|
indexOf | 128926.2 Ops/sec |
includes | 129125.2 Ops/sec |
find | 2887.7 Ops/sec |
findIndex | 3391.1 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark definition provides the context for the test cases. In this case, we have a JavaScript array arr
with a specific size (100,000 elements) that is initialized in the script preparation code.
Script Preparation Code
var arr = [];
var i = 0;
while (i <= 1E5) arr[i] = i++;
This code initializes an empty array arr
and populates it with values from 0 to 100,000 using a while loop.
Html Preparation Code
The html preparation code is empty, which means that the test cases are executed in isolation, without any additional HTML context.
Individual Test Cases
We have four test cases that compare different methods for finding an element in the array:
indexOf
includes
find
findIndex
Let's break down each test case and explain what is being tested:
indexOf
indexOf
searches for a specific value (1E5
) in the array and returns its index if found, or -1 if not.
Library: None
Pros:
Cons:
includes
includes
checks if a specific value (1E5
) exists in the array.
Library: Array.prototype.includes()
This method was introduced in ECMAScript 2015 and provides a more concise way to check if an element exists in an array, compared to using indexOf()
or iterating through the array manually.
Pros:
indexOf
Cons:
find
find
searches for a specific value (1E5
) in the array and returns the first matching element or undefined if no match is found.
Library: Array.prototype.find()
This method was introduced in ECMAScript 2015 and provides a more concise way to find an element in an array, compared to iterating through the array manually using forEach()
or indexOf()
.
Pros:
indexOf()
or iterating through the arrayCons:
findIndex
findIndex
searches for a specific value (1E5
) in the array and returns its index if found, or -1 if not.
Library: Array.prototype.findIndex()
This method was introduced in ECMAScript 2015 and provides a more concise way to find an element in an array and return its index, compared to using indexOf()
or iterating through the array manually.
Pros:
indexOf()
or iterating through the arrayCons:
Other Considerations
When comparing these methods, it's essential to consider the specific use case and requirements of your application. For example:
includes()
might be a good choice.find()
might be a better option.findIndex()
or indexOf()
might be suitable.Alternatives
Some alternative methods for finding elements in arrays include:
forEach()
to iterate through the array manuallymap()
or filter()
to transform the array into a new array with specific valueslodash
or Ramda
.