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()
}), {});
Object.entries(data).reduce((acc, [k, v]) => {
acc.set(k, v.toString())
return acc;
}, new Map());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) | |
Map |
Test name | Executions per second |
---|---|
Object.fromEntries | 445.4 Ops/sec |
Reduce (reuse object) | 744.3 Ops/sec |
Reduce (creating temporary objects) | 28.1 Ops/sec |
Map | 802.0 Ops/sec |
Measuring the performance of different approaches to create an object from entries in JavaScript is a common task, especially when optimizing code for various use cases.
Benchmark Overview
The provided benchmark compares four approaches:
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
Map
Each approach has its own set of trade-offs and considerations.
Approach 1: Object.fromEntries
Object.fromEntries
is a relatively new method introduced in ECMAScript 2019 (ES10). It creates an object from an iterable of key-value pairs, where each pair is provided as an array with the first element being the key and the second element being the value.
Pros:
Object.fromEntries
uses the built-in Object.entries()
method to get the entries from the data object.Cons:
Object.fromEntries
is supported in ES10, older browsers may not have it implemented. Chrome 114, the benchmarked browser, has implemented it.Approach 2: Reduce (reuse object)
This approach uses the reduce()
method to iterate over the data entries and create an object with the same structure. To maintain the reuse of objects, the accumulator is initialized as an empty object ({}
), and each iteration appends a new key-value pair.
Pros:
Cons:
reduce()
can lead to performance issues due to increased memory allocation and garbage collection.Approach 3: Reduce (creating temporary objects)
Similar to Approach 2, but instead of reusing an existing object, a new one is created on each iteration of reduce()
.
Pros: None notable.
Cons: The approach suffers from the same performance overhead and code complexity issues as Approach 2.
Approach 4: Map
The Map
data structure provides efficient storage for key-value pairs. In this approach, the Object.entries()
method is used to get the entries from the data object, which are then iterated over using a map's forEach()
method or iterating directly on its entries.
Pros: Efficient iteration and direct access to key-value pairs.
Cons: Additional memory allocation due to creating a new map instance, although more efficient than Approach 2 and 3 when handling large datasets.
In summary:
Object.fromEntries
is an efficient and intuitive approach for creating objects from entries but may be limited in older browsers.Reduce (reuse object)
offers control over the object creation process but comes with potential performance overhead due to repeated object allocations.Reduce (creating temporary objects)
shares the same performance issues as Approach 2.Map
provides efficient iteration and direct access to key-value pairs while requiring additional memory allocation.Other alternatives could include:
Array.of()
or new Int8Array()
, if available in older browsers.Object.assign()
or Object.create()
.WeakMap
for specific use cases.Keep in mind that the choice of approach depends on the project's requirements, performance constraints, and browser support.