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 | 86.4 Ops/sec |
Reduce (reuse object) | 734.7 Ops/sec |
Reduce (creating temporary objects) | 29.7 Ops/sec |
Let's break down the provided benchmark and explain what is tested, the options compared, pros and cons of each approach, and other considerations.
Benchmark Overview
The benchmark measures the performance difference between three approaches for creating an object from an array of key-value pairs:
Object.fromEntries
Array.prototype.reduce()
with a custom callback function that creates a new objectArray.prototype.reduce()
with a custom callback function that directly assigns values to the accumulator objectScript Preparation Code
The script preparation code creates an object data
containing 10,000 key-value pairs using Array.from(Array(10000).keys())
.
Benchmark Definition JSON
The benchmark definition json provides three test cases:
Object.fromEntries
: uses Object.fromEntries()
to create the object from the array of key-value pairs.Reduce (reuse object)
: uses Array.prototype.reduce()
with a custom callback function that creates a new object, reusing the accumulator (acc
) for each iteration.Reduce (creating temporary objects)
: uses Array.prototype.reduce()
with a custom callback function that directly assigns values to the accumulator object, creating a new object in each iteration.Library Used
In this benchmark, the Object.fromEntries()
method is used from the ECMAScript standard library.
Special JS Feature/Syntax
None mentioned explicitly. However, it's worth noting that the use of arrow functions (e.g., (key, value) => [key, value]
) in the benchmark definition json demonstrates a modern JavaScript feature.
Options Compared
The three test cases compare different approaches to creating an object from an array of key-value pairs:
Object.fromEntries()
: uses a built-in method that is designed for this specific use case.Reduce (reuse object)
: uses the Array.prototype.reduce()
method with a custom callback function, reusing the accumulator object for each iteration.Reduce (creating temporary objects)
: uses the same Array.prototype.reduce()
method as above, but with a different implementation that creates a new object in each iteration.Pros and Cons of Each Approach
Object.fromEntries()
:Reduce (reuse object)
:Reduce (creating temporary objects)
:Other Considerations
When choosing between these approaches, consider the following factors:
Object.fromEntries()
is likely to be the fastest option due to its optimized implementation.Reduce (reuse object)
might be a better choice. However, this approach can lead to code complexity and maintenance issues.Alternatives
If you don't have access to modern JavaScript features like Object.fromEntries()
, you can explore other alternatives:
Array.prototype.reduce()
with a custom callback function that creates a new object in each iteration.However, keep in mind that these alternatives may not offer the same level of performance or conciseness as the original benchmark.