var array = Array.from(Array(10000).keys())
Object.fromEntries(array.map(value => [value, value]));
const data = {}
array.forEach(value => data[value] = value);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
creating temporary objects |
Test name | Executions per second |
---|---|
Object.fromEntries | 1714.0 Ops/sec |
creating temporary objects | 14253.9 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing two approaches to create objects in JavaScript:
Object.fromEntries
with an array of key-value pairs.forEach
method.Options Being Compared
Object.fromEntries
: A built-in JavaScript method introduced in ECMAScript 2015 (ES6), which creates a new object from an iterable of key-value pairs.Pros and Cons
Object.fromEntries
:Object.fromEntries
Library Usage
There is no explicit library usage in this benchmark. However, the Array.from()
method is used to create an array from another iterable (in this case, an array of numbers).
Special JS Features/Syntax
Other Considerations
The benchmark results are based on the Chrome 90 browser running on a desktop Windows platform. The test is designed to measure the execution speed of each approach, with multiple executions per second reported.
Alternative Approaches
Other approaches to create objects in JavaScript could include:
Array.prototype.reduce()
or other aggregation methodsHowever, these alternatives are not being tested in this specific benchmark.