var arr = [];
for (let i = 1; i <= 10000; i++) {
arr.push({ id: i, name: `${i}` });
}
var map = new Map(arr.map(item => [item.id, item]));
new Map(arr.map(item => [item.id, item]));
Array.from(map.values());
[map.values()]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Map | |
Array.from | |
Spread Operator |
Test name | Executions per second |
---|---|
new Map | 1589.6 Ops/sec |
Array.from | 43764.4 Ops/sec |
Spread Operator | 44083.9 Ops/sec |
Let's break down the provided JSON data and explain what is being tested.
Benchmark Definition: The benchmark measures the performance difference between three approaches:
new Map()
- Creating a new Map object from an array.Array.from()
, followed by map()
on a new or existing Map object (i.e., getting values from a Map repeatedly).[...map.values()]
- Using the spread operator to get values from a Map.Options Compared:
Pros and Cons:
Library and Special JS Features:
The benchmark uses the Map
data structure from JavaScript's built-in Object
namespace. No special features are required to run this benchmark.
Other Considerations:
When designing such a benchmark, it's essential to consider factors like:
Keep in mind that these results might vary depending on the specific JavaScript engine, browser, or runtime environment being used.
Alternatives:
Other alternatives for comparing these approaches include:
However, the spread operator approach remains one of the fastest ways to get values from a Map object in JavaScript.