Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS findIndex vs indexOf
(version: 0)
JS findIndex vs indexOf
Comparing performance of:
Get item by index with Array.indexOf vs Get item by index with Array.findIndex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Get item by index with Array.indexOf
const index = arr.indexOf(1E5) const item = arr[index]
Get item by index with Array.findIndex
const index = arr.findIndex(item => item === 1E5); const item = arr[index]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Get item by index with Array.indexOf
Get item by index with Array.findIndex
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 JavaScript microbenchmark test case. **What is being tested?** The benchmark tests two ways to retrieve an item from an array in JavaScript: 1. `Array.indexOf()`: This method returns the index of the specified value (in this case, 100,000) if it exists in the array. 2. `Array.findIndex()`: This method returns the index of the first element that satisfies the provided condition (in this case, a strict equality check with 100,000). **Options compared** The two options being compared are: 1. **Linear search**: Both methods use a linear search approach to find the specified value in the array. However, `Array.indexOf()` stops searching as soon as it finds the value, while `Array.findIndex()` continues searching even after finding the value. 2. **Early return vs. continue searching**: The main difference between `Array.indexOf()` and `Array.findIndex()` is their behavior when they don't find the specified value. **Pros and Cons** 1. **Array.indexOf()**: * Pros: + Faster, as it stops searching as soon as it finds the value. + More straightforward to understand and implement. * Cons: + Can return -1 if the value is not found, which might be a problem for some use cases. 2. **Array.findIndex()**: * Pros: + Continues searching even after finding the value, so it can be useful when you need to process all elements that match the condition. * Cons: + Slower, as it searches the entire array even if it finds the value early. In general, `Array.indexOf()` is faster and more suitable for cases where you only need to find the first occurrence of a value. `Array.findIndex()`, on the other hand, is useful when you need to process all elements that match the condition. **Library usage** Neither `Array.indexOf()` nor `Array.findIndex()` use any external libraries. **Special JS feature or syntax** There is no special JavaScript feature or syntax used in this benchmark. However, it's worth noting that both methods work with modern browsers and Node.js, which means you don't need to worry about older browser versions. **Other alternatives** If you're looking for alternative ways to retrieve an item from an array in JavaScript, you might consider using: 1. **Binary search**: If the array is sorted and you know a range of possible values, binary search can be faster than linear search. 2. **Array.prototype.some()**: This method returns `true` as soon as it finds the first element that matches the condition, similar to `Array.indexOf()`. Keep in mind that these alternatives might not be suitable for all use cases, and the choice of implementation depends on your specific requirements and constraints.
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf - JavaScript performance
JS find vs indexOf
findIndex vs indexOf for simple array 2
JS Array IndexOf vs includes vs findIndex vs find
Comments
Confirm delete:
Do you really want to delete benchmark?