Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some using numbers
(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 = [1, 2, 3,4,5,6,7,8,9,10]
Tests:
IndexOf
array.indexOf(7)
Includes
array.includes(7)
some
array.some(v => v === 7)
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 dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark is designed to compare the performance of three different methods for finding if an array contains a specific value: 1. `array.indexOf(value)` 2. `array.includes(value)` 3. `array.some(v => v === value)` These methods are commonly used in JavaScript, and their performance can vary depending on the browser and platform. **Options Compared** The three options being compared are: * `indexOf`: Returns the index of the first occurrence of the specified value. If the value is not found, it returns -1. * `includes`: Returns a boolean indicating whether the array includes the specified value. * `some`: Returns a boolean indicating whether at least one element in the array satisfies the provided condition. **Pros and Cons** Here's a brief overview of each method: * `indexOf`: + Pros: Can be faster for exact matches, as it returns an index value immediately. + Cons: May incur additional overhead if the array is large or the value is not found, leading to unnecessary computations. * `includes`: + Pros: Generally faster than `indexOf`, especially for larger arrays, since it doesn't require computing an index value. + Cons: May have slightly lower performance due to the need to iterate through the array. * `some`: + Pros: Can be more flexible, as it allows testing multiple conditions and can short-circuit early if the condition is not met. + Cons: Typically slower than `indexOf` and `includes`, especially for large arrays, since it requires iterating through the elements. **Library and Special JS Features** None of these methods rely on any external libraries or special JavaScript features. They are built-in array methods that are part of the ECMAScript specification. **Other Considerations** When choosing between these methods, consider the specific requirements of your use case: * If you need to find the index of a value for further processing, `indexOf` might be a better choice. * If you only care about whether the value is present in the array, `includes` could be sufficient. * If you need to test multiple conditions or want more control over the iteration process, `some` might be the way to go. **Alternative Approaches** Other alternatives for finding values in an array include: * Using a custom function with a loop: e.g., `function findValue(array, value) { for (let i = 0; i < array.length; i++) { if (array[i] === value) return true; } return false; }` * Using `Array.prototype.every()` and negating the result: e.g., `return !array.some(v => v !== value)` * Using a dedicated library or function for fast array lookups, such as `fast-integer-indexed-array` or `binary-search`. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the built-in methods.
Related benchmarks:
IndexOf vs Includes array of numbers
Array find with indexOf vs includes
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?