Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some 4 elements
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes vs some
Created:
3 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', 'something']
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
some
array.some(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
some
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, compared, and their pros and cons. **What is being tested?** The benchmark compares three ways to find if an array contains a value: 1. `array.indexOf('sausage') !== 1` 2. `array.includes('sausage')` 3. `array.some(v => v === 'sausage')` These methods are used to determine whether the string `'sausage'` exists in the array. **Comparison of options** Here's a brief overview of each option: ### 1. `array.indexOf('sausage') !== 1` * **Purpose**: Returns the index of the first occurrence of `'sausage'` in the array, or `-1` if not found. * **Pros**: + Simple and straightforward implementation. + Fast, as it uses a simple linear search. * **Cons**: + May return `NaN` (Not a Number) instead of `-1` for certain edge cases. + Does not account for null or undefined values. ### 2. `array.includes('sausage')` * **Purpose**: Returns `true` if the array includes `'sausage'`, and `false` otherwise. * **Pros**: + More concise and readable implementation than `indexOf`. + Works with null or undefined values, returning false instead of NaN. * **Cons**: + Slower than `indexOf` due to additional checks. + May be slower in older browsers. ### 3. `array.some(v => v === 'sausage')` * **Purpose**: Returns true if at least one element in the array is equal to `'sausage'`. * **Pros**: + More concise and expressive implementation than `indexOf` or `includes`. + Works with arrays of any type, not just strings. * **Cons**: + May be slower due to the use of a callback function. **Library: Lodash** The benchmark includes a reference to the Lodash library (`https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js`). Lodash is a utility library that provides a wide range of functions for working with arrays, objects, and other data structures. In this case, the `includes` function from Lodash is used in the benchmark. **Special JS features** There are no special JavaScript features or syntaxes being tested in this benchmark. All methods use standard JavaScript syntax and semantics. **Alternatives** Other alternatives to these methods could include: * Using a different library or framework that provides similar functionality (e.g., `Array.prototype.includes` is supported by modern browsers). * Implementing your own custom search function using a loop. * Using a more advanced data structure, such as a trie or a hash table. It's worth noting that the `some` method may be slower than the other two options due to the use of a callback function. However, it provides a concise and expressive way to solve this problem, making it a popular choice among developers.
Related benchmarks:
IndexOf vs Includes
array indexOf vs includes vs some aaa
array indexOf vs includes vs some corrected
array indexOf vs includes vs some vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?