Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performance v2
(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).fill().map((_, idx) => ({ id: idx })); var itemToFind = arr[13000];
Tests:
findIndex
arr.findIndex((item) => item.id === itemToFind.id);
indexOf
arr.indexOf(itemToFind);
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/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
1627.2 Ops/sec
indexOf
981832.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Description** The benchmark is comparing two JavaScript methods to find an element in an array: 1. `arr.findIndex((item) => item.id === itemToFind.id)` 2. `arr.indexOf(itemToFind)` Both methods are used to find the index of a specific element (`itemToFind`) within a large array (`arr`). **Options Being Compared** The two options being compared are: * `findIndex`: This method returns the index of the first element in the array that satisfies the provided callback function. If no such element is found, it returns `-1`. * `indexOf`: This method returns the index of the first occurrence of the specified value (`itemToFind`) within the array. If the value is not found, it returns `-1`. **Pros and Cons of Each Approach** **findIndex:** Pros: * More flexible, as it allows for a custom callback function to filter elements. * Can be more efficient when dealing with large arrays, as it only iterates over the elements that might match. Cons: * May have higher overhead due to the need for a callback function and potential type checking. * Returns `-1` if no element is found, which may not be desirable in all cases. **indexOf:** Pros: * Typically faster and more efficient than `findIndex`, as it uses a simpler lookup algorithm. * Returns the index directly, without the need for a callback function or filtering. Cons: * Less flexible than `findIndex`, as it only allows for a direct value comparison. * May not work correctly with non-numeric values (e.g., strings), unless using a custom implementation. **Library Usage** None of the benchmarked methods rely on any external libraries. However, it's worth noting that some implementations of these methods might use internal helper functions or algorithms from the JavaScript standard library. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. Both `findIndex` and `indexOf` are part of the standard JavaScript API and do not require any advanced language constructs. **Other Alternatives** If you need to find an element in an array, other alternatives might include: * Using a loop: `for (var i = 0; i < arr.length; i++) { if (arr[i].id === itemToFind.id) { ... } }` * Using a regular expression: `arr.some((item) => new RegExp(`^${itemToFind.id}$`).test(item.id))` * Using a third-party library like Lodash or Ramda, which provide more advanced array manipulation functions. Keep in mind that these alternatives may have different trade-offs and performance characteristics compared to the standard `findIndex` and `indexOf` methods.
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf - JavaScript performancedsadsadas
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?