const fooSet = new Set();
for(let i=0;i<2000;i++){
fooSet.add(i);
}
const arrayFromSet = Array.from(fooSet)
const fooSet = new Set();
for(let i=0;i<2000;i++){
fooSet.add(i);
}
const arrayFromSet = [fooSet]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from | |
spread |
Test name | Executions per second |
---|---|
Array.from | 38124.6 Ops/sec |
spread | 38169.8 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark is comparing two ways to convert a Set object to an Array:
Array.from()
method...
)Options Compared
We have two options being compared:
Array.from(fooSet)
fooSet
...
) directly on the Set object, we're spreading its elements into a new array.Pros and Cons
Array.from()
:...
):Library
There is no library being used in this benchmark. However, the Set
object and its methods are part of the JavaScript standard library.
Special JS Feature or Syntax
None mentioned. The benchmark only uses standard JavaScript features and syntax.
Alternative Approaches
Other ways to convert a Set to an Array could include:
Array.prototype.forEach()
method: const array = []; fooSet.forEach((element) => { array.push(element); });
Keep in mind that these alternative approaches might be less efficient and more complex than the options being compared in this benchmark.