Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.indexOf vs array.includes vs array.some vs equality
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
array.indexOf vs array.includes vs array.some vs equality
Created:
2 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'] var value = 'sausage';
Tests:
array.indexOf
array.indexOf(value) !== 1
array.includes
array.includes(value)
array.some
array.some(v => v === value)
equality
'banana' === value || 'sausage' === value || jesus === 'value'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array.indexOf
array.includes
array.some
equality
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.indexOf
15460332.0 Ops/sec
array.includes
15603358.0 Ops/sec
array.some
10950476.0 Ops/sec
equality
16639557.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The `MeasureThat.net` website is used to create and run JavaScript microbenchmarks. The current benchmark compares the performance of different methods to find if an array contains a value: `array.indexOf`, `array.includes`, `array.some`, and equality checks using string comparisons (`'banana' === value || 'sausage' === value || jesus === 'value'`). **Options Compared** 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.includes**: This method checks if the array includes the specified value and returns a boolean result (true or false). 3. **array.some**: This method tests whether at least one element in the array passes a provided test. 4. **Equality Checks** (`'banana' === value || 'sausage' === value || jesus === 'value'`): These are simple string comparisons using the `===` operator. **Pros and Cons of Each Approach** 1. **array.indexOf**: * Pros: Fast for finding a specific element in an array, returns the index of the found element. * Cons: Returns -1 if not found, can be slower than other methods for very large arrays. 2. **array.includes**: * Pros: Simple and efficient way to check if an element is present in an array, returns a boolean result. * Cons: May be slower than `indexOf` or equality checks for very large arrays. 3. **array.some**: * Pros: Allows testing multiple conditions on each array element, can be faster than individual comparisons. * Cons: Returns a boolean result indicating whether at least one element passes the test, may not be suitable for finding a specific element. 4. **Equality Checks** (`'banana' === value || 'sausage' === value || jesus === 'value'`): * Pros: Simple and easy to understand, can be faster than array methods for very small arrays. * Cons: May be slower than array methods for large arrays, may not handle null or undefined values well. **Library Used** The benchmark uses the `lodash.js` library, specifically the `lodash.some()` function. The script preparation code includes a line to load this library from a CDN. **Special JS Features/Syntax** There is no specific JavaScript feature or syntax mentioned in the benchmark that requires special attention. All test cases use standard JavaScript methods and operators. **Other Alternatives** For finding if an array contains a value, other alternatives could include: * Using `Array.prototype.includes()` method (not available in older browsers) * Implementing a custom binary search algorithm * Using a data structure like a hash table or set to quickly look up values However, for most use cases, the methods compared in this benchmark (`array.indexOf`, `array.includes`, and equality checks) are sufficient.
Related benchmarks:
array indexOf vs includes vs some
array indexOf vs includes vs some aaa
array indexOf vs includes vs findIndex
array indexOf vs includes vs some vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?