Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some corrected
(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 = ['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:
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 break down the provided benchmark and explain what is being tested. **Benchmark Overview** The test compares three different ways to check if an array contains a specific value: `array.indexOf()`, `array.includes()`, and `array.some()` (with a corrected implementation). **Library Used** In this benchmark, Lodash.js is used. It's a popular JavaScript utility library that provides various functions for tasks like string manipulation, array operations, and more. **Comparison of Options** 1. **`array.indexOf()`**: This method returns the index of the first element in the array that matches the specified value. If no match is found, it returns -1. The test checks if `indexOf()` can detect when the value is not present in the array. 2. **`array.includes()`**: This method returns a boolean indicating whether an array includes any element equal to the provided argument. In this case, we're checking if `includes()` detects when the value is present or absent from the array. 3. **`array.some()` (with a corrected implementation)**: The original `some()` method returns a boolean indicating whether at least one element in the array passes the test implemented by the provided function. We're testing an adjusted version of this method, which correctly checks if the value is present or not. **Pros and Cons** * **`array.indexOf()`**: Pros: + Simple to implement. + Fast for arrays with a small number of elements. * Cons: + Returns -1 when no match is found, which can lead to incorrect results in some cases (e.g., if the value is used as an index). * **`array.includes()`**: Pros: + Returns true or false immediately, making it faster than `indexOf()`. + Handles edge cases like arrays with a single element correctly. * Cons: None notable, but may have performance overhead due to its method call. * **`array.some()` (corrected implementation)**: Pros: + Accurate detection of the value's presence or absence. + No need for a separate loop or conditional statement, making it concise and readable. **Special Considerations** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you needed to compare alternative methods, some examples could be: * Using `array.includes()` with the `String.prototype.toLowerCase()` method (for case-insensitive matching) * Employing a custom loop or iteration using `forEach()`, `map()`, and other array methods * Utilizing more advanced data structures like sets or maps for efficient lookups Keep in mind that these alternatives may have different performance characteristics, trade-offs in readability or maintainability, or require additional resources (e.g., memory).
Related benchmarks:
IndexOf vs Includes
array IndexOf vs array Includes vs lodash indexOf
array indexOf vs includes vs some aaa
array indexOf vs includes vs some 4 elements
array indexOf vs includes vs some vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?