Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some 2
(version: 2)
performance comparison of ways to find if an array contains a value
Comparing performance of:
indexOf vs includes vs some
Created:
3 years ago
by:
Registered User
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 = ['bananasausasesus', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage', 'sausageesusagsagausagejeusausagsusage', 'findMe', 'sausageesusagsagausagejeusausagsusage', 'jesusjesusausage']
Tests:
indexOf
array.indexOf('findMe') !== 1
includes
array.includes('findMe')
some
array.some(v => v === 'findMe')
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 benchmark and explain what's being tested. **Benchmark Purpose** The benchmark measures the performance of three different ways to check if an array contains a specific value: 1. `array.indexOf()` (IndexOf Method) 2. `array.includes()` (Includes Method) 3. `array.some()` (Some Method) Each method is used to search for the presence of the string "findMe" in the pre-defined array. **Options Compared** The benchmark compares the performance of these three methods: * **IndexOf Method**: This method returns the index of the first occurrence of the specified value, or -1 if it's not found. * **Includes Method**: This method returns a boolean indicating whether the specified value is present in the array. * **Some Method**: This method returns a boolean indicating whether at least one element in the array satisfies the provided condition (in this case, `v === 'findMe'`). **Pros and Cons of Each Approach** 1. **IndexOf Method** * Pros: Returns an index if found, which can be useful for further processing. * Cons: May be slower than other methods since it has to search the entire array from the start. 2. **Includes Method** * Pros: Simple, efficient, and returns a boolean value. * Cons: May return false positives (e.g., if the array contains other values with the same string) or false negatives (if "findMe" is not present). 3. **Some Method** * Pros: Can stop searching as soon as it finds a match, making it suitable for large arrays. * Cons: Requires an additional function to be provided, which may add overhead. **Library and Special JS Features** The benchmark uses the Lodash library (`lodash.js`) in its HTML preparation code. However, no special JavaScript features or syntax are used in this benchmark. **Other Alternatives** If you need a faster alternative to `array.includes()`, you can use `String.prototype.indexOf()` instead, but keep in mind that it's also an array method and might not be as efficient for large arrays. Another option is using the `Set` data structure to store the array values and check for presence, which could potentially provide better performance. Here's a simple example of how you can use `Set`: ```javascript const set = new Set(array); set.has('findMe'); ``` However, this approach requires an additional data structure creation step, which might not be ideal for large arrays.
Related benchmarks:
IndexOf vs Includes
indexOf vs includes vs some - 20211114
my own array: indexOf vs includes vs some
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?