var fooSet = new Map();
for(var i=0;i<100;i++) {
fooSet.set(i, i);
}
var other = Array.from(fooSet);
var fooSet = new Map();
for(var i=0;i<10000;i++) {
fooSet.set(i, i);
}
var other = Array.from(fooSet);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map to Array for 100 Items | |
Map to Array for 10,000 Items |
Test name | Executions per second |
---|---|
Map to Array for 100 Items | 447339.8 Ops/sec |
Map to Array for 10,000 Items | 2804.2 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of converting Map()
instances to arrays using the Array.from()
method.
In other words, it's measuring how long it takes to iterate over a map and collect its values into an array. This benchmark is useful for understanding the performance characteristics of JavaScript's built-in data structures and methods.
Options being compared
There are two main options being compared:
Pros and Cons
Array.from()
: The most common way to convert a Map to an Array in modern JavaScript. It's generally considered the most efficient approach.for...of
loop or another method of your choice.Library usage
None of the test cases use any external libraries. The Array.from()
method is a built-in JavaScript function that converts a Map to an Array.
Special JS features or syntax
The benchmark doesn't rely on any specific special JavaScript features or syntax beyond what's required for the Array.from()
function.
Other alternatives
If you wanted to measure the performance of converting Maps to Arrays using alternative methods, some options might include:
Array.from()
, you could use other constructors like Array.create()
or even new Array()
.for...of
loops, you could also explore using other iteration methods like forEach()
, map()
, or even Closures.Keep in mind that these alternatives would likely have different performance characteristics and might not be as efficient or readable as the original Array.from()
approach.