var obj = {
Array.from(Array(10000).keys())
};
Object.entries(obj).reduce((acc, [k, v]) => {
acc[k] = v;
return acc;
}, {});
Object.entries(obj).reduce((acc, [k, v]) => ({
acc,
[k]: v,
}), {});
Object.fromEntries(
Object.entries(obj).map(
([k, v]) => [k, v]
)
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce (object reuse) | |
reduce (no reuse) | |
fromEntries |
Test name | Executions per second |
---|---|
reduce (object reuse) | 786.5 Ops/sec |
reduce (no reuse) | 31.5 Ops/sec |
fromEntries | 402.5 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmarking test case, where the goal is to compare the performance of three different approaches for object mapping: reducing an object using Object.entries
and reduce
, with and without reusing the accumulator.
Test Cases
There are three test cases:
Object.entries
and reduce
, where the accumulator is reused.: This test case uses a more modern approach of creating an object from an array of key-value pairs using
Object.fromEntries` and mapping over the array to create individual key-value pairs.Options Compared
The three test cases are compared in terms of their performance, measured by the number of executions per second.
Pros and Cons
: This approach uses modern JavaScript features to create an object from an array of key-value pairs. It may provide better performance than the traditional
reduce` approach, but its performance depends on the specific use case.Libraries and Features
None of the test cases use any external libraries or special JS features other than the built-in Object.entries
, Object.fromEntries
, and Array.prototype.map
methods.
Other Considerations
Alternatives
Other alternatives for comparing performance include:
Object.assign
instead of reduce