Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs includes vs map & indexOf
(version: 0)
Comparing performance of: findIndex vs includes vs map and indexOf
Comparing performance of:
findIndex vs includes vs map and indexOf
Created:
5 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);
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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 141 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
332709.5 Ops/sec
includes
331995.8 Ops/sec
map and indexOf
5178.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case, specifically designed to compare the performance of three methods: `findIndex`, `includes`, and `map` with `indexOf`. The benchmark aims to measure which method is the most efficient in finding an element within a large array. **Tested Options** The three options being compared are: 1. **`findIndex`**: A built-in JavaScript method that returns the index of the first occurrence of a specified value. 2. **`includes`**: A built-in JavaScript method that returns `true` if an element is present in the array, and `false` otherwise. 3. **`map` with `indexOf`**: This approach uses the `map()` method to transform the array elements into a new array of values, and then uses `indexOf()` to find the index of the target value within the resulting array. **Pros and Cons of Each Approach** 1. **`findIndex`**: * Pros: Fast and efficient for large arrays, as it only searches through the array once. * Cons: Returns `-1` if the element is not found, which might be unnecessary in some cases. 2. **`includes`**: * Pros: Simple to use and easy to read, with no need to worry about edge cases or index values. * Cons: Slower than `findIndex` for large arrays, as it scans through the array until finding a match. 3. **`map` with `indexOf`**: * Pros: Allows for more control over the transformation of elements, but may introduce unnecessary overhead due to the additional mapping step. * Cons: Requires creating an intermediate array, which can consume memory and slow down performance. **Library or Built-in Function** The `includes()` method is a built-in JavaScript function, introduced in ECMAScript 2015 (ES6). It is designed for simple membership testing and is typically faster than implementing custom logic for this purpose. **Special JS Feature or Syntax** There are no special features or syntaxes used in this benchmark. However, it's worth noting that the `map()` method was also introduced in ECMAScript 2015 (ES6) as a more expressive way to transform arrays. **Other Alternatives** For large datasets, alternative approaches might include: 1. **Binary search**: A more complex algorithm that can find an element in O(log n) time complexity. 2. **Hashing**: Using a hash table data structure to store and look up elements quickly (O(1) average time complexity). 3. **Sparse lookup tables**: Creating a precomputed lookup table for common values, which can speed up the search process. Keep in mind that these alternatives are more complex and may not be suitable for all use cases or languages. The chosen approach depends on the specific requirements and constraints of your project.
Related benchmarks:
findIndex vs map & indexOf
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?