Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some - 100 numbers, find middle
(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
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 = Array(100).map((el, idx) => idx.toString());
Tests:
IndexOf
array.indexOf('50') !== 1
Includes
array.includes('50')
some
array.some(v => v === '50')
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:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
6367136.5 Ops/sec
Includes
16217671.0 Ops/sec
some
5846000.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its various components. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, specifically designed to compare the performance of three different methods for checking if an array contains a specific value: `indexOf`, `includes`, and `some`. **Methods Being Compared** 1. **`array.indexOf(value)`**: 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(value)`**: This method returns a boolean indicating whether the specified value is present in the array. 3. **`array.some(predicate)`**: This method returns a boolean indicating whether at least one element in the array satisfies the predicate (a function that takes an element and returns a boolean). **Pros and Cons of Each Approach** 1. **`indexOf`**: * Pros: Fast, efficient, and widely supported. * Cons: Returns the index of the first occurrence, which might not be what you want if there are multiple matches. Also, it can throw an error if the value is not found. 2. **`includes`**: * Pros: Simple, concise, and easy to read. It's also more explicit than `indexOf`, as it returns a boolean result. * Cons: Can be slower than `indexOf` for very large arrays or when using strict equality checks (e.g., `===`). 3. **`some`**: * Pros: Allows you to extract the value from the array if it's found, which can be useful in certain scenarios. It's also more concise than writing a custom loop. * Cons: Can be slower than `indexOf` or `includes`, especially for very large arrays. **Library Used** The benchmark uses the popular utility library Lodash (version 4.17.5), specifically its `lodash.includes()` function, which is used in the "Includes" test case. **Special JavaScript Feature/Syntax** There are no special features or syntax mentioned in the provided JSON. However, it's worth noting that some browsers might optimize certain methods more than others due to their implementation or caching mechanisms. **Other Alternatives** If you need a faster alternative to `indexOf` for very large arrays, you could consider using: 1. **`binarySearch()`**: A method that uses a binary search algorithm to find the index of an element in an array. 2. **Custom loop**: Writing a custom loop that iterates over the array using a fast indexing technique, such as a pointer or a hashmap. Keep in mind that these alternatives might require more boilerplate code and may not be as efficient as the built-in methods. I hope this explanation helps you understand the benchmark and its various components!
Related benchmarks:
IndexOf vs Includes array of numbers
Number array indexOf vs includes vs some
array indexOf vs includes vs findIndex
array indexOf vs includes vs some using numbers
Comments
Confirm delete:
Do you really want to delete benchmark?