Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
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:
5 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']
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:
3 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
182660000.0 Ops/sec
Includes
183192752.0 Ops/sec
some
174937696.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what's being tested. **Benchmark Overview** The website `MeasureThat.net` provides a platform for users to create and run JavaScript microbenchmarks. The current benchmark compares three approaches to find if an array contains a value: `array.indexOf()`, `array.includes()`, and `array.some()`. **What is being tested?** In the provided benchmark definition, three test cases are defined: 1. `IndexOf`: Checks if the array contains a specific value using `array.indexOf('sausage')`. 2. `Includes`: Checks if the array contains a specific value using `array.includes('sausage')`. 3. `some`: Uses a callback function to check if any element in the array matches a condition (`v => v === 'sausage'`). **Options compared** The benchmark compares the performance of these three approaches: * `indexOf()`: Returns the index of the first occurrence of the specified value, or `-1` if it's not found. * `includes()`: Returns `true` if the array contains the specified value, and `false` otherwise. * `some()`: Returns `true` as soon as any element in the array matches the condition (in this case, `v === 'sausage'`), or `false` if no elements match. **Pros and Cons of each approach** 1. `indexOf()`: * Pros: Simple and straightforward. * Cons: May not be efficient for large arrays, as it has to iterate through the entire array to find the first occurrence. 2. `includes()`: ( Introduced in ECMAScript 2019) * Pros: Efficient and concise, as it uses a binary search algorithm under the hood. * Cons: Introduced relatively recently, so some older browsers may not support it. 3. `some()`: * Pros: Flexible and can be used to check for any condition, not just equality. * Cons: May be slower than `indexOf()` or `includes()`, especially for large arrays, since it has to iterate through the entire array. **Library usage** The benchmark includes a reference to the `lodash.js` library: ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` `Lodash` is a popular utility library for JavaScript that provides various helper functions, including ones for array manipulation. **Special JS feature or syntax** The benchmark uses the `some()` method with an arrow function (`v => v === 'sausage'`). This syntax was introduced in ECMAScript 2015 (ES6) and allows for concise and expressive code. The arrow function is equivalent to a traditional function declaration, but with some benefits, such as reduced overhead. **Other alternatives** If `indexOf()` or `includes()` are not suitable for the use case, other alternatives could be: * Manual iteration through the array using a loop * Using a different library or framework that provides an alternative implementation However, it's worth noting that these approaches may be slower and less efficient than the ones being compared in this benchmark.
Related benchmarks:
IndexOf vs Includes
array indexOf vs includes vs some aaa
array indexOf vs includes vs some 4 elements
find vs includes vs indexof
array indexOf vs includes vs some vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?