var set = new Set([1,41,23,12321,-3, .13, () => {}, {}, []]);
var arrFrom = Array.from;
[set];
arrFrom(set);
Array.from(set);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
cached array.from | |
uncached array.from |
Test name | Executions per second |
---|---|
spread operator | 6533459.0 Ops/sec |
cached array.from | 770969.0 Ops/sec |
uncached array.from | 2731311.8 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that provides information about the test case:
{
"Name": "fastest way to convert set to array",
"Description": null,
"Script Preparation Code": "var set = new Set([1,41,23,12321,-3, .13, () => {}, {}, []]);\r\nvar arrFrom = Array.from;",
"Html Preparation Code": null
}
Here's what's happening:
Set
object is created with some elements.Array.from()
method is assigned to the variable arrFrom
.Individual Test Cases
There are three individual test cases:
[
{
"Benchmark Definition": "[...set];",
"Test Name": "spread operator"
},
{
"Benchmark Definition": "arrFrom(set);",
"Test Name": "cached array.from"
},
{
"Benchmark Definition": "Array.from(set);",
"Test Name": "uncached array.from"
}
]
Each test case defines a different way to convert the Set to an array:
spread operator
: This uses the spread operator ([...set]
) to create a new array from the Set.cached array.from
: This uses the cached version of Array.from()
(since it was previously assigned to the arrFrom
variable).uncached array.from
: This uses the default, uncached version of Array.from()
.Options Compared
The benchmark is comparing three different approaches:
[...set]
)array.from()
array.from()
Each approach has its pros and cons:
array.from()
before using it, which may incur some overhead.Other Considerations
The benchmark is likely running on a variety of browsers and platforms, so the results may vary depending on the specific environment. Additionally, the use of new Set()
creates an immutable Set object, which can affect performance.
Libraries Used
None are explicitly mentioned in this benchmark definition.
Special JavaScript Features/Syntax
The spread operator ([...set]
) is a new feature introduced in ECMAScript 2015 (ES6). It allows creating a new array from an iterable source. This feature was not available in earlier versions of JavaScript and may require specific browser support.
That's a summary of the benchmark!