Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs set has (x-small list)
(version: 0)
Comparing performance of:
Array.includes vs Set.has vs ArrayProtoIncludes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ 'auto', 'no-flex', ]; var ArrayProtoIncludes = Array.prototype.includes; var ReflectApply = Reflect.apply; var aSet = new Set(array);
Tests:
Array.includes
var b = array.includes('no-flex') var c = array.includes('boop')
Set.has
var b = aSet.has('no-flex') var c = aSet.has('boop')
ArrayProtoIncludes
var b = ReflectApply(ArrayProtoIncludes, array, ['no-flex']); var c = ReflectApply(ArrayProtoIncludes, array, ['boop']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
Set.has
ArrayProtoIncludes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
15698956.0 Ops/sec
Set.has
15571068.0 Ops/sec
ArrayProtoIncludes
5073749.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The test cases measure the performance of three different approaches to check if an element exists in an array or set: 1. `Array.prototype.includes()`: This method checks if a specified value (in this case, 'no-flex') exists in the array. 2. `Set.has()` method: This method is used to determine whether an element ('no-flex') exists within a set created from the same array as above. 3. `Reflect.apply(ArrayProtoIncludes, array, ['no-flex'])`: This approach uses the `Reflect.apply()` function to call the `includes()` method on the `ArrayProto` prototype (i.e., the built-in `Array.prototype` object), passing in the array and a string argument ('no-flex'). **Options compared:** * The three test cases compare the performance of each of these three approaches. * Another alternative is using the traditional `indexOf()` method for arrays, which is not included in this benchmark. However, since `includes()` has largely replaced `indexOf()` in modern browsers and JavaScript engines, it's worth noting that the results would likely be similar. **Pros and Cons:** 1. **Array.prototype.includes()**: * Pros: Wide browser support (including older versions), easy to use, and efficient for small arrays. * Cons: Can be slower than `Set.has()` for very large datasets due to the overhead of searching the array. 2. **Set.has() method**: * Pros: Efficient for very large datasets since sets provide O(1) lookup time on average, making it suitable for handling large lists or sets. * Cons: Requires a separate set data structure creation and can be slower than `Array.prototype.includes()` for smaller arrays due to the overhead of creating and searching the set. 3. **Reflect.apply(ArrayProtoIncludes, array, ['no-flex'])**: * Pros: Provides a more direct access to the built-in `includes()` method without having to create a separate function or prototype chain lookup. * Cons: May incur additional overhead due to the use of `Reflect.apply()`, which is not as well optimized as the native `includes()` method. **Libraries and their purpose:** * The `Set` library is used to create a set data structure, which is then used in conjunction with the `has()` method. This approach takes advantage of the O(1) lookup time provided by sets for large datasets. * There is no specific library mentioned in the benchmark definition that is not part of the standard JavaScript specification. **Special JS features or syntax:** There are no special features or syntax used beyond what's described above, such as ES6 arrow functions or async/await, which are commonly used in modern JavaScript development.
Related benchmarks:
convert to set + set.has vs. array.includes
array.includes vs set.has for small n
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?