Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf vs includes - JavaScript performance
(version: 0)
Comparing performance of:
findIndex vs indexOf vs includes
Created:
3 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 = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex((num) => num === foo);
indexOf
var index = arr.indexOf(foo);
includes
var index = arr.includes(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
findIndex
indexOf
includes
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
24973.3 Ops/sec
indexOf
520703.7 Ops/sec
includes
519228.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark that compares the performance of three different methods: `findIndex`, `indexOf`, and `includes` (all part of the Array prototype) when searching for an element in an array. **Test Cases** The test cases are: 1. `findIndex`: Finds the index of the first occurrence of the specified value in the array. 2. `indexOf`: Finds the index of the first occurrence of the specified value in the array, returning -1 if not found. 3. `includes`: Returns a boolean indicating whether an element with the specified value exists in the array. **Options Compared** The benchmark compares three different approaches to achieve the same goal: * `findIndex`: This method is generally faster than `indexOf` because it stops iterating as soon as it finds the matching element, whereas `indexOf` continues searching even after finding a match. * `indexOf`: As mentioned earlier, this method is slower than `findIndex`, but still relatively fast. It's often used when you want to know if an element exists in the array, regardless of its index. **Pros and Cons** Here are some pros and cons of each approach: * `findIndex`: + Pros: Fast, efficient, and concise. + Cons: May not be suitable for all use cases (e.g., when you need to know the exact index). * `indexOf`: + Pros: Suitable for most use cases where you just want to know if an element exists in the array. + Cons: Slower than `findIndex`, but still relatively fast. **Library and Syntax Considerations** In this benchmark, no special libraries or syntax are used. However, it's worth noting that some browsers may have different implementations of these methods, which could affect performance. **Other Alternatives** If you need to search for an element in an array and don't care about its index, `includes` might be a suitable alternative to `indexOf`. If you need the index or want more control over the search process, you can use a simple loop or `findIndex`. In summary, `findIndex` is generally faster than `indexOf`, but may not be suitable for all use cases. `indexOf` provides a convenient way to check if an element exists in the array, while `includes` offers a concise alternative when you just need to know if an element is present. **Benchmark Result Interpretation** The latest benchmark result shows that: * `findIndex` performs the best on this specific test case. * `indexOf` follows closely behind, with a slightly lower execution rate than `findIndex`. * `includes` has the lowest execution rate among the three options. These results suggest that `findIndex` is likely to be the fastest option for this specific use case. However, it's essential to keep in mind that benchmark results can vary depending on the specific browser and hardware being used.
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?