Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex (Array prototype methods) vs indexOf
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex vs Array.prototype.indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Array.prototype.find
const item = arr.find(item => item == 1E5);
Array.prototype.findIndex
const index = arr.findIndex(item => item == 1E5);
Array.prototype.indexOf
const index = arr.indexOf(item => item == 1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.find
Array.prototype.findIndex
Array.prototype.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.find
3220.3 Ops/sec
Array.prototype.findIndex
3211.9 Ops/sec
Array.prototype.indexOf
142395.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Analysis** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, which compares the performance of three different methods for searching an array in JavaScript: `find`, `findIndex`, and `indexOf`. **Methods Compared** 1. **`Array.prototype.find()`**: This method returns the first element in the array that satisfies the provided testing function. 2. **`Array.prototype.findIndex()`**: This method returns the index of the first element in the array that satisfies the provided testing function. If no element satisfies the condition, it returns -1. 3. **`Array.prototype.indexOf()`**: This method returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. **Pros and Cons** * `find()`: Pros: * Simple to use * Can be used with functions that return values other than a boolean (e.g., finding an object based on multiple properties) * Allows for more flexibility in the search criteria Cons: * Returns the first element that satisfies the condition, not necessarily the first match * Can lead to performance issues if the array is very large and the testing function returns a value frequently * Does not return an index, which can be inconvenient if you need it * `findIndex()` Pros: * Returns the index of the first element that satisfies the condition, making it more convenient for subsequent operations * More efficient than `find()` because it stops as soon as it finds a match * Does not return an index if no element satisfies the condition Cons: * Can be less intuitive to use than `find()` * Returns -1 instead of null or undefined if no element is found * `indexOf()` Pros: * Easy to use and understand * Returns the first occurrence of the specified value, making it suitable for simple searches * Does not require a function as the second argument Cons: * Less flexible than `find()` or `findIndex()` * Can be slower due to its simple implementation **Library Usage** None of these methods rely on external libraries. **Special JS Feature or Syntax** The benchmark uses JavaScript's built-in array methods, which do not require any special features or syntax beyond standard ECMAScript syntax. **Alternatives** If you need to search arrays in JavaScript, consider the following alternatives: * **`Array.prototype.some()`**: Returns true if at least one element in the array satisfies the provided testing function. * **`Array.prototype.every()`**: Returns true if all elements in the array satisfy the provided testing function. * **Regular expressions (`/regex/`)**: Can be used to search for patterns within arrays, but may not offer the same level of flexibility as `find()` or `findIndex()`. * **Custom loops**: Writing a custom loop to iterate over an array and perform a comparison can also achieve the goal, but is often less efficient than using built-in methods.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) with console.log
find vs findIndex (Array prototype methods) 22
find vs findIndex (Array prototype methods) stop at first found
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?