Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array .indexOf vs .some vs .includes
(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
Script Preparation code:
var array = ['cat', 'house', 'ate', 'bingo', 'mango', 'crypto', 'trump', 'banana', 'sausage', 'jesus']
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
29088640.0 Ops/sec
Includes
28771246.0 Ops/sec
some
29511646.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark compares the performance of three different methods to find if an array contains a specific value: `array.indexOf()`, `array.some()`, and `array.includes()`. **What are we testing?** We're testing how fast each method can return a boolean result indicating whether a certain element is present in the array. In other words, we want to know which method is the fastest for this specific use case. **Options compared** The three methods being tested are: 1. `array.indexOf()`: This method returns the index of the first occurrence of the specified value in the array, or -1 if it's not found. 2. `array.some()`: This method returns true as soon as it finds an element in the array that satisfies the provided callback function. If no elements satisfy the condition, it returns false. 3. `array.includes()`: This method returns a boolean value indicating whether the specified value is present in the array. **Pros and Cons of each approach** 1. **`array.indexOf()`**: * Pros: Fast and efficient when the element is found at the beginning of the array. * Cons: Can be slow if the element is not found, as it has to search the entire array. 2. **`array.some()`**: * Pros: Efficient when only one or a few elements need to be checked, as it stops iterating as soon as it finds a match. * Cons: May have overhead due to the callback function and iteration. 3. **`array.includes()`**: * Pros: Simple and easy to use, with minimal overhead. * Cons: Can be slower than `indexOf()` for large arrays, especially if the element is not found at the beginning. **Library usage** None of the methods mentioned require a specific library, but if we were using a library like Lodash, we might use the `findIndex()` function, which is similar to `array.indexOf()`, or the `some()` function, which is equivalent to the callback-based implementation used in the benchmark. **Special JS feature** The benchmark uses a JavaScript feature called "arrow functions" (e.g., `v => v === 'sausage'`) for the `some()` method. Arrow functions are a shorthand way of defining small functions, and they're supported by most modern browsers. **Other alternatives** In addition to these three methods, other ways to check if an element is present in an array include: 1. **`map()`**: While not exactly what you want, `map()` can be used with the `includes()` method or a callback function that checks for presence. 2. **`filter()`**: Similar to `map()`, but it's more commonly used for creating new arrays rather than checking for existence. 3. **Manual loop**: A simple loop using `for` or `forEach()` can be used to check each element in the array. Keep in mind that these alternatives might have different performance characteristics and use cases, so the benchmark is specifically testing the three methods mentioned.
Related benchmarks:
IndexOf vs Includes
array indexOf vs includes vs some using numbers
array indexOf (gt -1) vs includes vs some
my own array: indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?