var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// var b = new Set(a)
return a.includes(9)
var b = new Set(a)
return b.has(9)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 8410854.0 Ops/sec |
lookup | 438596.0 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Definition
The benchmark is comparing two approaches:
includes
: Using the array.includes()
method to check if an element exists in an array.lookup
: Using a Set
data structure to store elements and then checking if an element exists using the has()
method.Options Compared
The two options being compared are:
includes
) vs. Set-based approach (lookup
)Pros and Cons of Each Approach
Array-based approach (includes
)
Pros:
Cons:
Set-based approach (lookup
)
Pros:
includes()
for very small arrays (less than 10 elements).Cons:
Other Considerations
The benchmark assumes that the array will always contain more than one element, as mentioned in the Script Preparation Code (// var b = new Set(a)
). This is because the includes()
method can return false for empty arrays or arrays with only one element, whereas has()
requires a non-empty Set.
Library Used
In this benchmark, no libraries are used. However, note that if you were to use a library like Lodash, it might offer an optimized implementation of includes()
called lodash.isInclude()
, which could potentially change the results.
Special JS Feature or Syntax
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of two different approaches using standard language constructs (array.includes()
and Set.has()
).