Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performancedsadsadas
(version: 0)
Comparing performance of:
findIndex vs indexOf vs map indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(10); 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);
map indexOf
var index = arr.map((e) => e.id).indexOf(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
findIndex
indexOf
map 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 dive into the explanation of the provided benchmark. **Benchmark Overview** The test case measures the performance difference between three different approaches to find an element in an array: 1. `findIndex()` 2. `indexOf()` 3. `map()` followed by `indexOf()` **Library Used: None** There is no library explicitly mentioned in the benchmark definition or the individual test cases. However, we can assume that the standard JavaScript Array methods are being used. **Approaches Compared** 1. **findIndex():** This method returns the index of the first element in the array that satisfies the provided condition (in this case, equality with `foo`). If no element satisfies the condition, it returns -1. 2. **indexOf():** This method is similar to `findIndex()`, but it performs a loose equality check using the Strict Equality Operator (`===`). It also returns -1 if no element matches the specified value. 3. **map()` followed by `indexOf():** In this approach, we create a new array with mapped values (in this case, just the index of each element in the original array), and then search for the specified value using `indexOf()`. **Pros and Cons** 1. **findIndex()**: Pros: * More efficient than `indexOf()` since it uses strict equality, which can reduce false positives. * Returns -1 instead of a non-existent index, making it easier to handle edge cases. Cons: * Requires the provided value (`foo`) to be present in the array for it to work correctly. 2. **indexOf():** Pros: + Can find elements even if they are not equal to `foo` (in terms of strict equality). + Returns a non-negative index, making it easier to handle edge cases. Cons: + May return false positives due to loose equality. + Requires more computations compared to `findIndex()`. 3. **map()` followed by `indexOf():** Pros: * Can be used even if the original array contains non-numeric values (since we're mapping over indices). * Returns -1 if no index matches, making it easier to handle edge cases. Cons: + Requires an additional operation (`map()`) that can increase overhead. + May have a higher computational cost compared to `findIndex()` or `indexOf()` alone. **Other Considerations** * The benchmark uses the `Math.floor(Math.random() * 15000)` method to generate a random index (`foo`). This is done to ensure that the condition in each test case has an equal chance of being met. * The array size used in the test cases (10 elements) might be too small to represent all possible scenarios. Increasing the array size could provide more accurate results. **Alternatives** If you wanted to explore alternative approaches, here are a few options: 1. **using `filter()`**: Instead of using `findIndex()` or `indexOf()`, you could use `filter()` to find elements that match a certain condition. 2. **using `forEach()`**: You could also use `forEach()` in conjunction with a callback function to achieve the same result as `map()` followed by `indexOf()`. 3. **using `binary search`**: If the array is sorted or can be easily sorted, you could use binary search to find an element more efficiently than the above approaches. These alternatives might provide different performance characteristics or handling of edge cases compared to the original test case.
Related benchmarks:
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?