Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performance 2
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); for (var i = 0; i < 15000; i++) { arr.fill({ id: i }); } numArr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex((obj) => obj.num === foo);
indexOf
var index = numArr.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:
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 provided JSON and explain what's being tested, compared, and other considerations. **What is being tested?** The benchmark measures the performance of two JavaScript methods: `indexOf` and `findIndex`. These methods are used to find the index of a specific element within an array. The test creates a large array of 15,000 elements with a unique identifier (`id`) for each element, and then uses these arrays in both tests. **Options compared** The two options being compared are: 1. `indexOf` 2. `findIndex` Both methods take two arguments: the array to search in and the value to search for. However, they differ in their approach: * `indexOf` returns the index of the first occurrence of the specified value within the array. * `findIndex` returns the index of the first occurrence of the specified value within the array, but it returns -1 if no element is found. **Pros and Cons** Here are some pros and cons for each approach: * `indexOf`: + Pros: Generally faster than `findIndex`, especially for small arrays. + Cons: Returns the index of the first occurrence of the value, which may not be what the developer expects if there are duplicate values in the array. * `findIndex`: + Pros: Returns the actual element that matches the specified value, which can be more useful in some scenarios. + Cons: Generally slower than `indexOf`, especially for small arrays. **Library and purpose** In this test case, the `map()` function is used to create a new array with the same elements as the original array, but with the indices assigned instead of the values. This creates two different arrays: 1. `arr`: The original array with unique identifiers (`id`) for each element. 2. `numArr`: A new array created by mapping the original array and assigning the indices to the values. **Special JavaScript feature or syntax** The `map()` function is a built-in JavaScript method that creates a new array with the results of applying a provided function on every element in this array. It's used here to create two different arrays, which are then used in both tests. **Other alternatives** There are other methods you could use to achieve similar results: 1. `Array.prototype.indexOf()`: This is an older method for finding the index of a value within an array. 2. `Array.prototype.findIndex()`: This is another method for finding the index of a value within an array, but it's generally slower than `indexOf`. 3. Using a loop to iterate through the array: You could use a simple loop to iterate through the array and find the index of the specified value. However, in this test case, the focus is on comparing the performance of `indexOf` and `findIndex`, so using a different approach would likely change the results.
Related benchmarks:
indexOf vs findIndex with a simple case
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?