var data = Object.entries({ Array.from(Array(10000).keys()) }).map((key, value) => [key, value]);
Object.fromEntries(data);
new Map(data)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Map |
Test name | Executions per second |
---|---|
Object.fromEntries | 449.5 Ops/sec |
Map | 3573.9 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark that compares the performance of two data structures: Object.fromEntries
and Map
. The benchmark is designed to measure which approach is faster for creating an object from an array of key-value pairs.
Script Preparation Code
The script preparation code creates a dataset by:
Array.from(Array(10000).keys())
.Object.entries
.The resulting data is then passed to both the Object.fromEntries
and Map
methods.
Options Compared
Two options are compared in this benchmark:
Pros and Cons of Each Approach
const obj = Object.fromEntries([...])
).Object.fromEntries
because it avoids the overhead of object creation and property access.const obj = new Map([...])
).Library/Function Used
None, as both methods are built into JavaScript.
Special JS Feature/Syntax
No special features or syntax are used in this benchmark. However, if you're interested in exploring other performance comparisons, MeasureThat.net also tests various other JavaScript methods and libraries.
Other Alternatives
If you're looking for alternatives to Object.fromEntries
and Map
, consider the following:
JSON.parse(JSON.stringify(data))
: A simple way to create an object from a JSON-like data structure.Array.prototype.reduce()
: Can be used to create an object by reducing an array of key-value pairs.Array.prototype.forEach()
: Can be used to iterate over key-value pairs in an array.Set
or Map
alternatives, such as Array.prototype.reduce()
or third-party libraries like Lodash.Keep in mind that the choice of method depends on your specific use case and requirements.