Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
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(0); arr = arr.map((el, idx) => el === idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex(e => e === 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:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
26946.9 Ops/sec
indexOf
118808.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark compares the performance of two methods: `findIndex` and `indexOf`. Both methods are used to find the index of a specific element (in this case, a random integer `foo`) within an array (`arr`). The benchmark aims to determine which method is faster and more efficient. **Options Compared** There are only two options being compared: 1. **`findIndex`**: A method introduced in ECMAScript 2012 (ES6) that finds the index of the first element in the array that satisfies the provided callback function. 2. **`indexOf`**: An older method that finds the index of a specified value within an array, returning `-1` if the value is not found. **Pros and Cons** * `findIndex`: + Pros: - More concise syntax - Can be used to find multiple indices at once (e.g., `arr.findIndex(e => e === foo && e === bar)`). + Cons: - May have slightly slower performance due to the additional check for the existence of the element. * `indexOf`: + Pros: - Older, widely supported method - Faster execution time in some cases (although this may not be noticeable for small arrays like `arr`) + Cons: - Less concise syntax **Other Considerations** When choosing between these two methods, consider the specific use case and performance requirements: * If you need to find a single index or perform multiple searches on an array, `findIndex` might be a better choice due to its conciseness and flexibility. * However, if you're working with very large arrays or need to ensure older browsers support your code, `indexOf` might be a more reliable option. **Library Usage** Neither `findIndex` nor `indexOf` uses a specific library. They are both built-in JavaScript methods that come with the language itself. **Special JS Features/Syntax (None)** There are no special JavaScript features or syntax used in this benchmark, other than those already mentioned (`ES6` feature for `findIndex`). **Alternatives** If you need to compare performance of other array methods or want to test different search algorithms, some alternatives could be: * `Array.prototype.includes()`: A more modern method introduced in ECMAScript 2015 (ES6) that checks if an element exists within the array without returning its index. * `Array.prototype.indexOf()` with a custom implementation: You can write your own version of `indexOf` to compare performance with the built-in method. In summary, this benchmark provides a simple and concise way to compare the performance of two common array methods in JavaScript. Understanding the pros and cons of each method will help you choose the most suitable approach for your specific use case.
Related benchmarks:
basic comparison - findIndex vs indexOf
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?