Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some vs for loop
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes vs some vs for loop
Created:
one year 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', 'foo', 'bar', 'baz'] var len = array.length
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
some
array.some(v => v === 'sausage')
for loop
for (let i = 0; i < len; i++) { if (array[i] === 'sausage') break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
some
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:147.0) Gecko/147.0 Firefox/147.0
Browser/OS:
Firefox Mobile 147 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
24478498.0 Ops/sec
Includes
24066540.0 Ops/sec
some
217715680.0 Ops/sec
for loop
206684896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The benchmark, titled "array indexOf vs includes vs some vs for loop," tests the performance of four different approaches to check if an array contains a specific value: 1. `indexOf()` 2. `includes()` 3. `some()` with a callback function 4. A traditional `for` loop **Approach 1: `indexOf()`** `indexOf()` returns the index of the first occurrence of a specified element in an array. If no such element exists, it returns `-1`. Pros: * Simple and straightforward * Native implementation in most browsers Cons: * Returns an index value, which might not be desirable if you're only interested in the presence or absence of the element * Can be slower than other methods for large arrays **Approach 2: `includes()`** `includes()` returns a boolean indicating whether an array contains a specified element. Pros: * Simple and straightforward * Returns a boolean value, which is more convenient than an index * Faster than `indexOf()` for large arrays (thanks to its native implementation) Cons: * Not supported in older browsers **Approach 3: `some()` with a callback function** `som()` returns a boolean indicating whether at least one element of an array satisfies the provided testing function. Pros: * More flexible, as it allows you to perform more complex checks * Can be used for arrays of arbitrary types Cons: * Requires creating and passing a callback function, which can add overhead * Less straightforward than other methods **Approach 4: Traditional `for` loop** A traditional `for` loop iterates over the array elements, checking each one against the target value. Pros: * Highly customizable, as you can modify the loop to suit your needs * Can be more efficient for small arrays or when exact indexing is required Cons: * More verbose and error-prone than other methods * Requires manual iteration, which can lead to off-by-one errors **Library: Lodash** The benchmark uses a modified version of Lodash's `lodash.min.js` library. Lodash provides various utility functions, including `indexOf()` and `includes()`, which are used in the benchmark. Pros: * Simplifies code and reduces overhead for common tasks * Provides consistent behavior across browsers Cons: * Adds dependency on an external library * May not be suitable for all use cases due to its size and complexity **Special JS Feature/Syntax: None** This benchmark does not utilize any special JavaScript features or syntax. **Other Alternatives** If you're looking for alternative approaches, consider the following: * `filter()`: Returns a new array containing only elements that pass the test implemented by the provided function. * `every()`: Returns a boolean indicating whether all elements of an array satisfy the testing function. * `reduce()`: Applies a reduction function to each element in an array and returns a single output value. Keep in mind that these alternatives might not be directly comparable to the original methods tested in this benchmark.
Related benchmarks:
array indexOf vs includes vs some
array indexOf vs includes vs some aaa
array indexOf vs includes vs some 4 elements
array indexOf vs includes vs some corrected
Comments
Confirm delete:
Do you really want to delete benchmark?