Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performance (fixed)
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill(null); arr = arr.map((el, idx) => ({ id: idx })); var randomEl = arr[Math.floor(Math.random() * 15000)]; var randomId = randomEl.id;
Tests:
findIndex
var index = arr.findIndex((el) => el.id === randomId);
indexOf
var index = arr.indexOf(randomEl);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
1227.9 Ops/sec
indexOf
518102.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Benchmark Purpose** The benchmark on MeasureThat.net measures the performance difference between two JavaScript methods: `findIndex` and `indexOf`. Both methods are used to find the index of an element in an array that matches a specific condition. However, they have different implementations and behaviors. **Options Compared** Two main options are compared: 1. **`findIndex`**: The `findIndex` method returns the index of the first element in the array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. 2. **`indexOf`**: The `indexOf` method returns the index of the first occurrence of a specified value in the array. If the value is not found, -1 is returned. **Pros and Cons** Here are some key points to consider for each option: * **`findIndex`** * Pros: * More flexible: allows for any predicate function as the second argument. * Better performance: often faster than `indexOf` due to its ability to stop searching once it finds a match. * Cons: * May return -1 instead of null if no elements satisfy the condition. * Can be slower if the array is very large and the predicate function has side effects (e.g., iterating over the entire array). * **`indexOf`** * Pros: * Returns null if no elements are found, which can be a more intuitive result than -1. * Often faster for simple lookups with known values. * Cons: * Less flexible: only allows for exact value matching. **Library and Special JS Features** In this benchmark, the `map` function is used to create an array of objects with specific properties. The `Math.random()` function is used to generate a random element and its corresponding index from the generated array. No special JavaScript features or syntax are used in this benchmark other than the standard ECMAScript 5+ features supported by modern browsers. **Other Alternatives** If you need to find the index of an element in an array, consider using: * `filter()` followed by `indexOf()`: This approach is less efficient than using `findIndex` directly but can be useful if you need to filter out elements before finding the index. * A native array method or a library function: Some programming languages and libraries offer more optimized methods for finding indices in arrays. Here's a simple example of how to use `filter()` followed by `indexOf()```` const arr = [1, 2, 3]; const randomEl = arr[Math.floor(Math.random() * arr.length)]; const index = arr.filter((el) => el === randomEl).indexOf(randomEl); ``` Keep in mind that this approach can be slower than using `findIndex` due to the overhead of filtering and indexing. Overall, `findIndex` is generally a better choice when you need a flexible and performance-optimized method for finding indices in arrays.
Related benchmarks:
findIndex vs indexOf on array of objs
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf - JavaScript performancedsadsadas
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?