var data = Object.entries({ Array.from(Array(10000).keys()) });
data.reduce((map, [price, vol]) => {
map[price] = vol;
return map;
}, {});
Object.fromEntries(data);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce with same obj | |
fromEntries |
Test name | Executions per second |
---|---|
reduce with same obj | 8969.2 Ops/sec |
fromEntries | 2288.9 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and discussed.
Benchmark Definition
The benchmark is focused on measuring the performance difference between two approaches: reducing an array of objects into a single object using Array.prototype.reduce()
and creating the same object using Object.fromEntries()
. The input data consists of 10,000 key-value pairs stored in an array of arrays, where each inner array contains a price and a volume.
Script Preparation Code The script preparation code defines the input data:
var data = Object.entries({ ...Array.from(Array(10000).keys()) });
This creates an object with 10,000 key-value pairs, where each key is a unique integer from 0 to 9,999 and the corresponding value is undefined
.
Html Preparation Code There is no HTML preparation code provided.
Options Being Compared
The two options being compared are:
Array.prototype.reduce()
: This method applies a reduction function to each element in the array and accumulates a result. In this case, it's used to create an object with price as keys and volume as values.Object.fromEntries()
: This method creates a new object from an iterable of key-value pairs.Pros and Cons
Array.prototype.reduce()
: Pros:Object.fromEntries()
for complex operations.Object.fromEntries()
: Pros:Array.prototype.reduce()
.Library In this benchmark, there are no external libraries being used.
Special JavaScript Features or Syntax
There are no special features or syntax mentioned in the provided code. The standard JavaScript reduce()
and Object.fromEntries()
methods are used without any additional options or customizations.
Other Alternatives
For comparison purposes, other alternatives to these two options could include:
Array.prototype.forEach()
and a callback function to iterate over the array and create an object.Array.prototype.map()
to create an array of key-value pairs and then using Object.fromEntries()
or reduce()
to create the final object.However, it's worth noting that these alternatives would likely introduce more overhead due to additional memory allocations and operations.