Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs includes
(version: 0)
Comparing performance of: findIndex vs includes
Comparing performance of:
findIndex vs includes vs map and indexOf
Created:
4 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 =1;
Tests:
findIndex
var index = arr.findIndex(item=>item.id===foo);
includes
var index = arr.map(item=>item.id).includes(foo);
map and indexOf
var index = arr.map(o => o.id).indexOf(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
findIndex
includes
map and 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 world of JavaScript microbenchmarks. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: `findIndex`, `includes`, and `map` followed by `indexOf`. The test creates an array of 15,000 elements with unique IDs, maps each element to its ID, and then performs a search using one of the three approaches. **Options Compared** The three options being compared are: 1. **`findIndex`**: This method returns the index of the first occurrence of the specified value, or -1 if no match is found. 2. **`includes`**: This method checks if an element with the specified value exists in the array. It returns `true` if a match is found and `false` otherwise. 3. **`map` followed by `indexOf`**: The `map` method applies a function to each element of the array, returning a new array with the transformed values. Then, the `indexOf` method searches for the specified value in the resulting array. **Pros and Cons** 1. **`findIndex`**: * Pros: Fastest approach, as it uses binary search under the hood. * Cons: Returns -1 if no match is found, which might be misleading if you're expecting a non-negative index. 2. **`includes`**: * Pros: Simple and intuitive, with a clear result (true or false). * Cons: Can be slower than `findIndex`, especially for large arrays. 3. **`map` followed by `indexOf`**: * Pros: Allows for more flexible searching, as the mapped values can be any type of object. * Cons: Requires extra memory and CPU cycles to perform the mapping and indexing. **Library** The test uses no external libraries beyond the built-in JavaScript functions. However, it's worth noting that some browsers may have additional optimizations or implementations for these functions that might affect performance. **Special JS Features/Syntax** None of the benchmarked code snippets use any special JavaScript features or syntax that would require explanation. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **`filter()`**: Instead of `map` followed by `indexOf`, you could use the `filter()` method to create an array with only the elements matching the specified value. 2. **`forEach()`**: Another approach would be to use the `forEach()` method to iterate over the array and perform the search manually, using a simple loop or a custom iterator. These alternatives might offer different trade-offs in terms of performance, memory usage, and code readability, so it's worth experimenting with each approach to see which one works best for your specific use case.
Related benchmarks:
Array find with indexOf vs includes
test 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?