Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Array IndexOf vs find vs findIndex vs includes
(version: 0)
Comparing performance of:
find vs findIndex vs indexOf vs includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
find
const item = arr.find(item => item == 1E5);
findIndex
const index = arr.findIndex(item => item == 1E5);
indexOf
const index = arr.indexOf(1E5);
includes
const included = arr.includes(1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
find
findIndex
indexOf
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different methods to find an element in an array: 1. `indexOf` 2. `includes` 3. `findIndex` 4. `find` These methods are used to search for a specific value within an array. **Options Compared** Each method has its own way of searching for the element, and we'll compare their performance: * **`indexOf`**: This method returns the index of the first occurrence of the specified value within the array. If the value is not found, it returns `-1`. * **`includes`**: This method checks if the array includes the specified value without returning an index. * **`findIndex`**: Similar to `indexOf`, but this method only returns the index of the first occurrence of the specified value in a strictly increasing sequence. If no such element is found, it returns `-1`. * **`find`**: This method returns the first element that satisfies the provided testing function. **Pros and Cons** Here's a brief summary of each method's pros and cons: * **`indexOf`**: + Pros: Fast and efficient for small arrays. + Cons: May be slower for large arrays, as it has to iterate through the entire array. * **`includes`**: + Pros: Simple and straightforward implementation. + Cons: May not be as efficient as `indexOf` or `findIndex`, especially for large arrays. * **`findIndex`**: + Pros: Similar performance to `indexOf` for small arrays and may be more efficient than `includes`. + Cons: Requires the array elements to be in a strictly increasing sequence, which can be a limitation. * **`find`**: + Pros: Allows for more flexible testing conditions (e.g., finding the first element that meets a certain condition). + Cons: May have higher overhead due to its more complex implementation. **Library and Purpose** None of these methods rely on external libraries. They are native JavaScript functions. **Special JS Feature or Syntax** * None of these methods use special JavaScript features or syntax beyond basic JavaScript programming. **Other Alternatives** For searching arrays, other alternatives include: * `Array.prototype.some()` for a more concise implementation * Using `Binary Search` for very large arrays (not tested in this benchmark) * Using external libraries like Lodash or Ramda for array manipulation and filtering The provided benchmark should give an idea of how these methods compare in terms of performance, but keep in mind that actual performance may vary depending on the specific use case and environment.
Related benchmarks:
JS find vs indexOf
findIndex vs indexOf for simple array 2
JS Array IndexOf vs includes vs findIndex vs find
Array find with indexOf vs includes
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?