Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
basic comparison - findIndex vs indexOf
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
5 years 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) => el.id); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex(x=>x===foo);
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
33533.5 Ops/sec
indexOf
520345.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared options, pros and cons of each approach, special considerations, alternative approaches, and more. **What's being tested:** The test is designed to compare the performance of two JavaScript methods: `Array.prototype.findIndex()` and `Array.prototype.indexOf()`. The goal is to find the index of a specific element (`foo`) within an array (`arr`) containing 15,000 elements with unique IDs. **Options compared:** 1. `findIndex()`: This method returns the index of the first element in the array that satisfies the provided condition (in this case, `x===foo`). If no such element exists, it returns -1. 2. `indexOf()`: This method searches for the specified value (`foo`) within the array and returns its first occurrence's index. If the value is not found, it returns -1. **Pros and Cons:** * `findIndex()`: + Pros: - More flexible, as it can be used to find the first element that satisfies a condition (not just equality). - Returns an index, which can be useful for further operations. + Cons: - May perform better if the array is already sorted or has some other predictable structure. - Can throw an error if the provided function returns `undefined` or `null`. * `indexOf()`: + Pros: - Fast and efficient, especially for large arrays with a known value. - Simple and widely supported. + Cons: - Less flexible than `findIndex()`, as it only searches for exact equality. - Returns -1 if the value is not found, which might be undesirable in some cases. **Library and Special Considerations:** Neither `findIndex()` nor `indexOf()` relies on a specific library. However, it's essential to note that both methods are part of the ECMAScript standard and have been supported by most modern browsers for a long time. There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches:** Other approaches could involve: * Using `Array.prototype.reduce()` to find the index, which would involve more complex logic but potentially better performance. * Using `Binary Search` on the array, which would be even faster for large arrays but more complex to implement. * Using a library like Lodash or Ramda for optimized indexing functions. Keep in mind that these alternatives might not provide significant performance gains and could introduce additional complexity. **Benchmark Preparation Code:** The preparation code creates an array with 15,000 elements filled with unique IDs and generates a random index (`foo`) to use for the test. This ensures a consistent test case. I hope this explanation helps software engineers understand the benchmark definition, tested options, and their pros and cons!
Related benchmarks:
findIndex vs map & indexOf
findIndex vs indexOf on array of objs
findIndex vs indexOf - JavaScript performance
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?