var data = { Array.from(Array(10000).keys()) };
Object.fromEntries(Object.entries(data).map((key, value) => [key, value]));
Object.entries(data).reduce((acc, [k, v]) => {
acc[k] = v;
return acc;
}, {});
Object.entries(data).reduce((acc, [k, v]) => ({
acc,
[k]: v
}), {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) |
Test name | Executions per second |
---|---|
Object.fromEntries | 135.9 Ops/sec |
Reduce (reuse object) | 991.2 Ops/sec |
Reduce (creating temporary objects) | 46.0 Ops/sec |
Let's break down the provided benchmark JSON and explain what's being tested.
Benchmark Definition
The benchmark is defined by three test cases:
Object.fromEntries
: This test case creates an object from an array of key-value pairs using the Object.fromEntries
method.Reduce (reuse object)
: This test case uses the reduce
method to create a new object by iterating over the data
array and assigning each value to a property of the accumulator object.Reduce (creating temporary objects)
: This test case is similar to the previous one, but it creates a new temporary object for each iteration instead of reusing the same accumulator.Options compared
In this benchmark, two main approaches are being tested:
Object.fromEntries
method, which creates an object from an array of key-value pairs in a single operation.reduce
method to create a new object by iterating over the data
array and assigning each value to a property of the accumulator object.Pros and Cons
Library
In this benchmark, the Array.from()
method is used, which is a part of the ECMAScript standard and is widely supported in modern browsers. The reduce
method is also part of the ECMAScript standard and is widely supported.
Special JS feature or syntax
The Object.fromEntries
method was introduced in ECMAScript 2015 (ES6) and has since become a standard feature. It's a concise way to create an object from an array of key-value pairs.
Overall, this benchmark tests the performance of two different approaches to creating objects from arrays: using Object.fromEntries
versus using the reduce
method.