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.toString();
return acc;
}, {});
Object.entries(data).reduce((acc, [k, v]) => ({
acc,
[k]: v.toString()
}), {});
const res = {};
for (const [k,v] of Object.entries(data)) {
res[k] = v.toString();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) | |
for of |
Test name | Executions per second |
---|---|
Object.fromEntries | 360.2 Ops/sec |
Reduce (reuse object) | 3703.5 Ops/sec |
Reduce (creating temporary objects) | 140.4 Ops/sec |
for of | 3773.1 Ops/sec |
What is being tested?
The provided benchmark tests the performance of three different approaches to create an object from an array of key-value pairs:
Object.fromEntries
: a built-in JavaScript method that creates an object from an iterable (in this case, an array) of key-value pairs.Reduce
with an object as the accumulator: in this approach, the reduce
method is used to create an object by iterating over the array and accumulating values into the accumulator object.Reduce
creating temporary objects: similar to the previous approach, but instead of using the accumulator object directly, a new temporary object is created at each iteration.Options compared
The three approaches are being tested against each other for performance differences:
Object.fromEntries
: a built-in method that creates an object from an iterable.Reduce (reuse object)
: uses the object as the accumulator and accumulates values into it directly.Reduce (creating temporary objects)
: creates a new temporary object at each iteration of the reduce function.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Object.fromEntries
:Reduce (reuse object)
:Reduce (creating temporary objects)
:Library usage
None of the provided benchmark code uses external libraries. However, if you're interested in exploring other approaches, some popular JavaScript libraries for working with objects and arrays include:
reduce
function that can be used as an alternative to the built-in method.mapValues
function that creates an object from an array of key-value pairs.Special JS features or syntax
The benchmark doesn't explicitly use any special JavaScript features or syntax, but it does rely on:
Array.from
: creates an array from an iterable.Object.entries
: returns an array of key-value pairs for an object.reduce
: a method that applies a function to each element of an array and accumulates the results.Other alternatives
If you're looking for alternative approaches, here are some additional options:
for...of
loop with Object.assign()
: creates an object from an iterable by iterating over it using a for...of
loop and assigning values to a new object.Array.prototype.map()
and Object.assign()
: transforms the array into an object by mapping each element to a key-value pair and then merging the results into a new object.These alternatives can be used as inspiration or as part of further benchmarking to explore performance differences.