var map = new Map();
var n = 1000000;
var obj = {};
for (let i = 0; i < n; i++) {
map.set(i, i);
obj[i] = i;
}
const values = Array.from(map.values());
const values = Object.values(obj);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map | |
obj |
Test name | Executions per second |
---|---|
map | 140.9 Ops/sec |
obj | 197.9 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net. The benchmark consists of two test cases: one for using a Map object and another for using an Object object.
Options Compared
In this benchmark, two approaches are compared:
Both options are used in the same way: creating an empty Map or Object, setting n
number of key-value pairs using a loop, and then retrieving all values from the map or object using Array.from()
for Map or Object.values()
for Object.
Pros and Cons of Each Approach
Library Used
In this benchmark, the Array.from()
function is used to retrieve all values from both Map and Object objects. This function is a part of the modern JavaScript standard library and is widely supported by browsers.
Special JS Feature or Syntax
None mentioned in this specific benchmark, but it's worth noting that the use of Array.from()
and Object.values()
is a relatively recent addition to the JavaScript language and may require support for ECMAScript 2019 (ES10) or later.
Other Alternatives
If you need to retrieve values from an object without using Object.values()
, other alternatives include:
values()
function.Object.values()
using a loop and indexing into the object's prototype chain.Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to using Array.from()
or Object.values()
.