Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some vs Set has
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
some vs Set
Created:
4 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 = ['x26','x52','x164','x2844','x2869','x2870','x2873',,'x2876','x2962','x2980','x2986','x3179','x3243','x3248','x3288','x3289','x3322','x3333','x3397','x3417','x3423','banana', 'sausage', 'jesus']
Tests:
some
array.some(v => v === 'sausage')
Set
new Set(array).has('sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
some
Set
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 benchmarking test cases. **What is being tested?** MeasureThat.net is testing the performance of different approaches to find if an array contains a specific value: 1. `array.indexOf(v => v === 'sausage')`: This uses the `indexOf` method with a callback function. 2. `new Set(array).has('sausage')`: This uses a `Set` data structure created from the array, and then checks for existence using the `has` method. **Options compared** The test compares two options: 1. **Callback-based `indexOf`**: This approach uses the `indexOf` method with a callback function that takes an element of the array as an argument. 2. **Set-based `has`**: This approach creates a `Set` data structure from the array and then checks for existence using the `has` method. **Pros and Cons** 1. **Callback-based `indexOf`**: * Pros: Widely supported, simple to implement. * Cons: May be slower than other approaches due to the need to iterate over the array. 2. **Set-based `has`**: * Pros: Can be faster for large datasets since sets only store unique elements. * Cons: Requires additional memory allocation for the set data structure. **Other considerations** When using these approaches, it's essential to note that: 1. The callback function in `indexOf` may not return immediately if the value is not found, as it will continue searching the array. This can lead to slower performance. 2. Sets are generally faster than arrays when it comes to membership testing, but they require more memory and can be slower for very large datasets. **Library used** In this benchmark, `lodash` (version 4.17.5) is included as a dependency in the HTML preparation code. It's likely being used elsewhere in the test suite, but not directly relevant to these specific test cases. **Special JavaScript feature or syntax** There are no special features or syntaxes being tested in these benchmarks. The focus is on comparing two common approaches for array membership testing. Now, let's look at some alternative approaches that could be included in a benchmark like this: * **`includes()` method**: This would compare the performance of using the `includes()` method with an iterator function. * **Native `findIndex()` method**: This would test the performance of using the `findIndex()` method to find the index of the first occurrence of a value in the array. * **Custom implementation using binary search**: This would involve implementing a custom binary search algorithm to find the first occurrence of a value in the array. These alternative approaches could provide additional insights into the performance characteristics of different membership testing methods.
Related benchmarks:
array indexOf vs includes vs some aaa
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some 4 elements
array indexOf vs includes vs some w/ largeish array
Comments
Confirm delete:
Do you really want to delete benchmark?