Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test findIndex vs includes vs map & indexOf
(version: 0)
Comparing performance of: findIndex vs includes vs map and indexOf
Comparing performance of:
findIndex vs includes
Created:
3 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.indexOf(foo);
includes
var index = arr.includes(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
includes
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 benchmark and explain what's being tested. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares the performance of three different methods to find an element in an array: 1. `findIndex` 2. `includes` 3. Using `map` and `indexOf` These methods are used to search for a specific value (`foo`) within the array (`arr`). The benchmark aims to determine which method is the most efficient. **Options compared** The three options being compared are: 1. **findIndex**: This method returns the index of the first occurrence of the specified value. If the value is not found, it returns `-1`. 2. **includes**: This method returns `true` if the array includes the specified value, and `false` otherwise. 3. **map** + **indexOf**: This approach involves using the `map` function to create a new array with the same values as the original array, but with an additional property (`id`) that increments for each element. Then, it uses the `indexOf` method to search for the value in the mapped array. **Pros and cons of each approach** 1. **findIndex**: This method is generally the most efficient, as it only needs to traverse the array once to find the index of the value. However, if the value is not found, it returns `-1`, which may incur additional overhead. 2. **includes**: This method is simpler and faster than `findIndex` because it can short-circuit as soon as it finds the value, rather than traversing the entire array. However, it requires a single pass through the array to determine if the value exists. 3. **map** + **indexOf**: This approach involves creating a new array with additional properties, which can incur significant overhead due to memory allocation and copying. While `indexOf` is fast on its own, combining it with a mapping function makes this approach less efficient than the other two. **Library usage** None of the provided benchmark code uses any external libraries or frameworks. **Special JavaScript features** The benchmark does not use any special JavaScript features or syntax beyond standard ECMAScript features. **Other alternatives** Some alternative approaches that could be used to search for an element in an array include: * Using `forEach` and checking for a boolean condition * Using `reduce` with an initial value of `-1` and incrementing the index on each iteration until the value is found * Using a binary search algorithm, which can be efficient for large arrays but requires additional overhead due to extra memory accesses. However, these alternatives are not being tested in this specific benchmark.
Related benchmarks:
findIndex vs map & indexOf
findIndex vs includes vs map & indexOf
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?