Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf on array of objs
(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(a => a.id === foo);
indexOf
var index = arr.indexOf({id: 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:
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):
Let's break down the test case and explain what's being tested. **What is being tested?** The test case is designed to compare the performance of two methods for finding an element in an array: `findIndex` and `indexOf`. Both methods are part of the ECMAScript standard, but they have some differences. **Options compared** There are only two options being compared: 1. **`findIndex`**: This method returns the index of the first element in the array that satisfies the provided testing function. It is called with a callback function that takes two arguments: `value` and `index`. 2. **`indexOf`**: This method returns the index of the first occurrence of the specified value in the array. It also uses an optional second argument, which specifies the starting point for the search. **Pros and Cons** * `findIndex`: + Pros: - Can be used to find the index of any element that meets a certain condition. - Less likely to throw errors if no matching element is found (it returns `-1` instead). + Cons: - It requires a callback function, which can make the code more complex and harder to read. * `indexOf`: + Pros: - Simpler than `findIndex`, as it doesn't require a callback function. + Cons: - Returns `-1` if no matching element is found, but this may not be desirable depending on the use case. **Library** There is no library being used in this test case. The code only uses built-in JavaScript methods and features. **Special JS feature or syntax** This test case does not use any special JavaScript features or syntax that are specific to a particular browser or version. However, it does rely on modern JavaScript features like `Array.prototype.fill()` and arrow functions (`=>`). **Other alternatives** If you wanted to compare the performance of these two methods in different browsers or versions, you might also consider using: * `Array.prototype.findIndex()` with an optional `thisArg` parameter (a feature introduced in ECMAScript 2018). * `Array.prototype.indexOf()` with a starting point for the search (e.g., `arr.indexOf(arr[arr.length - 1], arr.length - 2)`). Keep in mind that these alternatives might not be supported by older browsers or versions of JavaScript. **Benchmark preparation code** The script preparation code is designed to create an array of 15,000 objects with a unique `id` property. The `map()` function is used to modify each object in the array and add a new `foo` property with a random value between 0 and 14,999. This creates a scenario where both `findIndex` and `indexOf` will need to search for an element that meets a certain condition. **Individual test cases** Each individual test case is a separate benchmarking run. In the first case (`"findIndex"`), the callback function used in the benchmarking script is `(a, idx) => a.id === foo`, which checks if the `id` property of each object matches the value of `foo`. In the second case (`"indexOf"`), the callback function is simply `{ id: foo }`, which directly searches for an object with an `id` property equal to `foo`. I hope this explanation helps!
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf for simple array 2
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?