var test=[];
for(let i=0; i<2000;i++){
test[i] = i;
}
const set = new Set(test);
const arr = new Array(test);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
set | |
array |
Test name | Executions per second |
---|---|
set | 10149.6 Ops/sec |
array | 13563284.0 Ops/sec |
Let's break down the benchmark test case and explain what's being tested.
Script Preparation Code
The script preparation code is:
var test = [];
for (let i = 0; i < 2000; i++) {
test[i] = i;
}
This code creates an array test
with 2000 elements, each initialized to its index value (i
). This array will be used as input for both tests.
Benchmark Definition
The benchmark definition consists of two individual test cases:
"const set = new Set(test);"
"const arr = new Array(test);"
Both tests create a data structure from the prepared test
array: one uses an instance of the built-in Set
object, while the other uses an instance of the built-in Array
object.
What's being tested?
The benchmark is testing the performance difference between creating an array and creating a set using JavaScript. Specifically:
Options compared:
There are two main options compared:
Set
object to create a set from the array.Array
constructor to create an instance of an array from the prepared array.Pros and Cons:
Array
due to the overhead of maintaining a set data structure.Set
, especially for large datasets.Library usage
Neither test uses a third-party library. The built-in JavaScript objects (Set
and Array
) are used directly in the benchmark.
Special JS feature or syntax
There is no special JavaScript feature or syntax mentioned in this benchmark. It's a simple, straightforward test of performance between two common data structures.
Other alternatives
If you wanted to add more variations to this benchmark, you could consider testing:
Map
, WeakSet
) instead of the built-in Set
and Array
.Keep in mind that introducing more variations can make the benchmark more complex and harder to interpret.