var a = new Array(1e5);
var lastIndex = a.length - 1;
for(i=0, I = a.length; i < I; i++) {
a[i] = i;
}
var b = new Set(a);
return a.includes(lastIndex)
return b.has(lastIndex)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
has |
Test name | Executions per second |
---|---|
includes | 9883.5 Ops/sec |
has | 7428904.5 Ops/sec |
Let's break down what's being tested in the provided JSON benchmark.
The benchmark is comparing two approaches for checking if an element exists in a data structure: set.has()
and array.includes()
. The test case uses two arrays of size 1e5 (100,000 elements) with values from 0 to 99,999. The array a
is prepared by setting each element to its index value.
Approaches being compared:
set.has()
: This method is part of the JavaScript Set
object, which automatically removes duplicates and provides fast membership testing.array.includes()
: This method is used for searching an element within a given array. It iterates through the array to find the specified value.Pros and Cons:
set.has()
:Set
object, which may not be suitable for all use cases. Also, this method only works on JavaScript engines that support Set
.array.includes()
:The test users special JS feature or syntax: None mentioned.
Other considerations:
Set
for this specific task may not be necessary, as the array already contains unique values. However, using Set
can provide a more efficient solution if you need to perform membership testing on a set of elements.Alternatives:
For smaller datasets, you might want to consider using other methods like:
Map
or an object for fast lookup.in
operator with arrays or sets.However, for large datasets like in this benchmark, set.has()
will likely outperform array.includes()
due to its O(1) time complexity.