Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
my own array: indexOf vs includes vs some
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes vs some
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
IndexOf
array.indexOf('sausage')
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 provided benchmark and explain what's being tested, compared, and some pros/cons of each approach. **Benchmark Overview** The benchmark is comparing three different methods to check if an array contains a specific value: `indexOf()`, `includes()`, and `some()`. **Library: None** There are no external libraries used in this benchmark. The array is created using JavaScript's built-in syntax. **Special JS Feature/Syntax: Some** The `some()` method uses the "ternary operator" or "conditional expression" feature of JavaScript, which allows you to write a concise way to perform conditional checks. This feature is commonly used for short-circuit evaluations and can be an efficient way to stop evaluating expressions as soon as a condition is met. **Comparison** The three methods being compared are: 1. `indexOf()`: This method returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. 2. `includes()`: This method returns a boolean value indicating whether the array includes the specified value or not. 3. `some()` : This method returns a boolean value indicating whether at least one element in the array satisfies the provided condition. **Pros and Cons of Each Approach** * `indexOf()`: + Pros: Fast and efficient, especially for arrays with unique values. + Cons: Returns the index of the first occurrence, which might not be what you want if there are multiple occurrences. If the value is not found, it returns -1, which can be a useful return value, but may also indicate an absence rather than a specific negative result. * `includes()`: + Pros: Easy to read and understand, straightforward implementation, and efficient for most cases. + Cons: May not be as fast as `indexOf()` for large arrays with unique values. It's also worth noting that `includes()` was introduced in ECMAScript 2019 and might require polyfills for older browsers. * `some()`: + Pros: Can stop evaluating expressions immediately if the condition is met, making it efficient for large arrays or when searching for a specific value within an array of many possibilities. + Cons: Requires using a function with a callback, which can make the code more difficult to read and understand. It also returns a boolean value, not the index or position. **Benchmark Result** The latest benchmark results show that: * `some()` is the fastest execution method among the three, executing 125 million times per second. * `includes()` comes in second, with around 43 million executions per second. * `indexOf()` takes the longest time to execute, around 38.7 million executions per second. Keep in mind that these results are browser-specific and may vary depending on other factors like system resources, CPU architecture, and software configuration. **Other Alternatives** There are other methods to check if an array contains a specific value, such as: * `filter()`: Returns an array of elements for which the provided function returns true. * `forEach()`: Executes the provided callback for each element in the array. * Regular expressions (`/regex/`): Can be used to search for patterns within arrays. However, these methods may not provide the same level of performance as `indexOf()` or `includes()` for simple checks. The choice of method depends on the specific use case and requirements.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes vs find
array indexOf (gt -1) vs includes vs some
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?