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({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex( (item) => item.id === foo);
indexOf
var bar = { id: foo }; var index = arr.indexOf(bar);
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll dive into explaining the benchmark. **What is being tested?** The benchmark is comparing two methods to find an element in an array: `Array.prototype.indexOf()` and `Array.prototype.findIndex()`. Both methods are used to search for a specific value within the array, but they differ in how they handle the result. **Options compared:** 1. **`indexOf()`**: This method returns the index of the first occurrence of the specified value, or -1 if it is not found. 2. **`findIndex()`**: This method returns the index of the first occurrence of the specified value, or -1 if it is not found. **Pros and Cons:** * `indexOf()`: Pros: + More widely supported in older browsers (IE 8+). + May be faster in cases where the element is found early in the array. * `findIndex()`: Pros: + Designed for use with promises, generators, or iterators, making it more flexible. + Returns -1 if not found, which can be useful for handling such cases. Cons: * `indexOf()`: May be slower due to its search algorithm and additional checks. * `findIndex()`: Can be slower for large arrays, as it has to iterate through the entire array to find a match. **Library usage:** None of the test cases use any libraries or external dependencies. The benchmark is purely focused on comparing the performance of these two methods within the Array prototype. **Special JS feature/syntax:** No special JavaScript features or syntax are used in this benchmark. **Other alternatives:** In addition to `indexOf()` and `findIndex()`, other methods can be used to search for an element in an array, such as: * `Array.prototype.includes()` (introduced in ECMAScript 2015): Returns true if the array includes the specified value. * Custom loop-based approaches using `for` loops or `forEach()`. Keep in mind that these alternatives may have different performance characteristics and are not being tested in this benchmark.
Related benchmarks:
findIndex vs map & indexOf
findIndex vs indexOf on array of objs
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?