var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = new Set(a)
a.includes(10)
b.has(10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array | |
set |
Test name | Executions per second |
---|---|
array | 82069936.0 Ops/sec |
set | 143665728.0 Ops/sec |
Let's dive into the provided JSON data and explore what is being tested.
Benchmark Definition
The benchmark definition is a simple comparison between two approaches: using an array (a
) vs using a Set (b
) to check if an element exists in a collection of numbers. The preparation code creates an array a
with 10 elements, and then creates a Set b
from the same array.
Test Cases
There are two test cases:
includes()
method on the array a
to check if the number 10
exists in the array.has()
method on the Set b
to check if the number 10
exists in the set.Benchmark Results
The latest benchmark results show that both approaches are executed approximately 19 million times per second on a Chrome browser running on Windows Desktop. The results indicate that there is no significant performance difference between using an array and a Set for this specific use case.
What's being compared?
In this benchmark, we're comparing the execution speed of two approaches to check if an element exists in a collection of numbers:
a
) with the includes()
methodb
) with the has()
methodPros and Cons
Here are some pros and cons of using arrays vs Sets for this use case:
Arrays:
Pros:
Cons:
includes()
) have a time complexity of O(n), where n is the length of the array. This can lead to slower performance for large arrays.Sets:
Pros:
has()
) have an average time complexity of O(1), making them faster than array lookups.Cons:
Other considerations
When choosing between using an array and a Set, consider the following factors:
Alternative approaches
Other alternatives to consider:
In summary, while there is no significant performance difference between using arrays and Sets in this specific use case, Sets offer better performance for large collections due to their O(1) lookup time complexity. When choosing between the two, consider factors like duplicates, performance, memory usage, and alternative approaches.