const set = new Set()
for (let i= 0; i < 10000; i++) {
set.add(i)
}
const arr = []
for (let i= 0; i < 10000; i++) {
arr.push(i)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set.add | |
Array.push |
Test name | Executions per second |
---|---|
Set.add | 2523.8 Ops/sec |
Array.push | 33637.4 Ops/sec |
Let's break down the benchmark definition and test cases to understand what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that provides metadata about the benchmark. It includes:
Name
: The name of the benchmark, which is "Array.push vs Set.add".Description
: An empty string, indicating no description is provided for this benchmark.Script Preparation Code
and Html Preparation Code
: These fields are empty, suggesting that no custom code needs to be executed before running the benchmark.Individual Test Cases
There are two test cases:
add()
method.push()
method.Options Compared
The benchmark compares two different approaches:
add()
method.push()
method.Pros and Cons
push()
are often optimized by JavaScript engines.Library and Syntax Considerations
There is no library used in these test cases, but it's worth noting that the use of Sets and arrays is part of the ECMAScript standard, which ensures compatibility across different JavaScript engines and platforms.
No special JS features or syntax are required for this benchmark. The tests only rely on basic JavaScript operations.
Other Alternatives
If you're looking for alternative data structures to compare with sets and arrays, some options include:
However, these alternatives may not be as widely supported or optimized by JavaScript engines as arrays and sets.