var fooMap = new Map();
for(var i=0;i<500;i++) {
fooMap.set(i, i);
}
var other = Array.from(fooMap.values());
var fooMap = new Map();
for(var i=0;i<500;i++) {
fooMap.set(i, i);
}
var other = [fooMap.values()];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from | |
Spread |
Test name | Executions per second |
---|---|
Array.from | 153871.5 Ops/sec |
Spread | 156006.9 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark measures the performance difference between two ways to extract values from a Map object in JavaScript: Array.from
and the spread operator (...
). The test case creates a large Map with 500 entries, sets each entry to its index value, and then extracts all values using both methods. The goal is to determine which method is faster.
Options Compared
Two options are compared:
fooMap.values()
is used as the argument.Pros and Cons
for
loops.Array.from
, especially for those not familiar with the spread operator.Libraries Used
None are explicitly mentioned in this benchmark. However, JavaScript's built-in Map
and Array
objects are used.
Special JS Features/Syntax
The test case uses modern JavaScript features:
Other Alternatives
If you were to implement this benchmark without using Array.from
or the spread operator, you could use:
for...of
loops or forEach()
to achieve similar results.Keep in mind that these alternatives would likely be less efficient than using Array.from
or the spread operator.
Benchmark Preparation Code
The script preparation code is not provided, but it's expected to create a large Map with 500 entries and set each entry to its index value. The HTML preparation code is also not provided, as it's likely generated by the benchmarking tool itself.
Overall, this benchmark provides an opportunity to compare two common approaches for extracting values from a Map object in JavaScript, helping users understand which method is faster and more efficient.