Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs findIndex
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes vs findIndex
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
findIndex
array.findIndex(v => v === 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
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 provided benchmark and its results. **What is tested?** The benchmark compares three different approaches to find if an array contains a specific value: 1. `array.indexOf('sausage') !== 1`: This method uses the `indexOf` function to search for the index of the specified element in the array. If the element is found, it returns its index; otherwise, it returns -1. The comparison with `!== 1` checks if the element is present at the expected position. 2. `array.includes('sausage')`: This method uses the `includes` function to check if the specified element is present in the array. 3. `array.findIndex(v => v === 'sausage')`: This method uses the `findIndex` function with a callback function to find the index of the first occurrence of the specified element. **Options compared** The benchmark compares these three approaches: 1. **`indexOf`**: A built-in JavaScript method that searches for the index of a specific value in an array. 2. **`includes`**: A more recent addition to the JavaScript standard library, introduced in ECMAScript 2015 (ES6), which provides a simpler and more efficient way to check if an array contains a specified value. 3. **`findIndex`**: Another built-in JavaScript method that finds the index of the first occurrence of a specific value in an array. **Pros and cons** 1. **`indexOf`**: * Pros: Widely supported, well-documented, and easy to use. * Cons: Can be slower than `includes` for large arrays, as it uses linear search. 2. **`includes`**: * Pros: Faster and more efficient than `indexOf`, especially for large arrays. It also provides a simpler syntax. * Cons: Less widely supported in older browsers or environments (pre-ES6). 3. **`findIndex`**: * Pros: More flexible and powerful than `indexOf`, as it allows searching from the beginning of the array. * Cons: Can be slower than `includes` for simple searches, as it iterates over the entire array. **Library usage** The benchmark uses the popular JavaScript utility library **Lodash**, version 4.17.5, which provides a `findIndex` function. Lodash is widely used and well-maintained, making it a good choice for this benchmark. **Special JS features or syntax** None mentioned in this specific benchmark. However, some older browsers might not support modern JavaScript features like `includes`. **Other alternatives** For comparing performance, other approaches could include: 1. Using `forEach` or `map` to iterate over the array and check each element manually. 2. Using a library like **Underscore.js**, which provides a similar set of utility functions to Lodash. 3. Implementing custom search algorithms, such as binary search for large arrays. Keep in mind that these alternatives might not be as efficient or well-documented as the standard methods and libraries used in this benchmark.
Related benchmarks:
IndexOf vs Includes
array indexOf vs includes vs some aaa
IndexOf vs Includes vs find
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?