var array=[];
for(let i=0; i<2000;i++){
array[i] = i;
}
var set = new Set(array);
set.has(1855);
array.includes(1855);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
set | |
array |
Test name | Executions per second |
---|---|
set | 26306.1 Ops/sec |
array | 15092034.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
The benchmark measures the performance difference between using an array's includes()
method versus creating a new Set object from the same array, and then checking if a specific element (1855) exists in it. The test case compares the execution times of these two approaches.
Options compared:
includes()
method: This is the first approach. It checks if a specified value (1855
) exists in the array.1855
) exists in it using the has()
method.Pros and Cons of each approach:
includes()
method:includes()
method.Other considerations:
Library or special JS feature:
None mentioned in the provided benchmark definition. However, it's worth noting that using a library like lodash
might provide an optimized implementation for the includes()
method, which could potentially change the outcome of the benchmark.
If you were to implement this benchmark yourself, consider using a testing framework and ensuring your code is well-optimized for performance.
Other alternatives:
Map
, to store unique elements instead of an array.includes()
method implementation for specific use cases or edge cases (e.g., checking if an element exists in a sorted array).To run this benchmark yourself, you would:
array
) and populate it with 2000 elements using a loop.includes()
and one for creating a Set object and checking its existence.