Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript perf test
(version: 1)
Comparing performance of:
findIndex vs indexOf
Created:
9 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => ({ id: idx })); var foo = { id: Math.floor(Math.random() * 15000) };
Tests:
findIndex
var index = arr.findIndex((el) => el.id === foo.id);
indexOf
var index = arr.indexOf(foo);
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:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
51048.2 Ops/sec
indexOf
873243.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The benchmark provided compares the performance of two different methods for searching through an array in JavaScript: `findIndex` and `indexOf`. ### Options Compared: 1. **findIndex**: - **Test Case**: `var index = arr.findIndex((el) => el.id === foo.id);` - This method iterates over the array and applies a provided testing function to each element. It returns the index of the first element that satisfies the condition defined in the function. If no elements satisfy the condition, it returns `-1`. 2. **indexOf**: - **Test Case**: `var index = arr.indexOf(foo);` - This method searches the array for the exact element passed to it (`foo` in this case) and returns the index of the first occurrence. If the element is not found, it also returns `-1`. ### Performance Results: According to the benchmark results: - The `indexOf` method executed approximately **873,243** operations per second. - The `findIndex` method executed approximately **51,048** operations per second. ### Pros and Cons: #### findIndex: - **Pros**: - It is more flexible than `indexOf`. You can define any condition for finding an element using a callback function. - It allows for more complex searches based on properties of an object, as shown in this benchmark where we look for an object's property (`id`). - **Cons**: - The overhead of invoking a callback function for each element causes it to be considerably slower than `indexOf`. #### indexOf: - **Pros**: - Generally faster for simple searches because it checks for exact matches without the overhead of a callback. - It is easier to use for primitive types (like numbers and strings) or for searching for exact objects if they are the same reference. - **Cons**: - Limited to searching for exact matches, which makes it less flexible than `findIndex`. - Cannot search based on conditions or properties. ### Other Considerations: When deciding which method to use, one must consider the specific requirements of the task at hand: - If performance is crucial, and you are searching for primitives or references, `indexOf` will often be the better choice. - If you need to search based on certain criteria (like properties of an object), then `findIndex` is necessary despite its performance cost. ### Alternative Approaches: Apart from `findIndex` and `indexOf`, there are other alternatives for searching in arrays: - **forEach**: You can iterate through the array manually with a `forEach` loop, allowing for complex logic. - **filter**: If you need to know all matches, you can use `filter` to get an array of all elements that satisfy a certain condition. - **for...of or traditional for loop**: These allow for fine-tuned control over the loop if more customization is needed. In summary, the choice between `findIndex` and `indexOf` hinges on the balance between performance and the complexity of the search for your specific use case. Understanding these differences can help engineers select the most appropriate array search method in JavaScript.
Related benchmarks:
findIndex vs indexOf
basic comparison - findIndex vs indexOf
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf - JavaScript performance 2
findIndex vs. indexOf
findIndex & indexOf on 2000 strings
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
findIndex vs indexOf vs includes- JavaScript perf
Comments
Confirm delete:
Do you really want to delete benchmark?