var data = Array.from(Array(10000).keys()).map(i => ({id: i, value: `val_${i}`}));
Object.fromEntries(data.map(obj => [obj.id, obj]));
data.reduce((acc, obj) => {
acc[obj.id] = obj;
return acc;
}, {});
data.reduce((acc, obj) => ({
acc,
[obj.id]: obj
}), {});
new Map(data.map(obj => [obj.id, obj]));
const res = {};
data.forEach(obj => {
res[obj.id] = obj;
});
const res = {};
for (const obj of data) {
res[obj.id] = obj;
}
const res = {};
for (let i = 0; i < data.length; i++) {
res[data[i].id] = data[i];
}
const res = {};
const length = data.length;
for (let i = 0; i < length; i++) {
res[data[i].id] = data[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) | |
Map | |
forEach | |
for of | |
for i | |
for i saved length variable |
Test name | Executions per second |
---|---|
Object.fromEntries | 1065.0 Ops/sec |
Reduce (reuse object) | 5617.8 Ops/sec |
Reduce (creating temporary objects) | 60.2 Ops/sec |
Map | 1419.4 Ops/sec |
forEach | 6459.1 Ops/sec |
for of | 7404.8 Ops/sec |
for i | 434.9 Ops/sec |
for i saved length variable | 631.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark is designed to compare various approaches for converting an array to an object. The test data consists of 10,000 objects with unique IDs and values.
Here's a brief overview of each approach:
Object.fromEntries(data.map(obj => [obj.id, obj]));
:
This method uses the Object.fromEntries()
function, which creates an object from an array of key-value pairs. The map()
function is used to extract the objects with their IDs as keys and values.Pros: concise and readable code.
Cons: might not be optimized for performance due to the use of the fromEntries()
function.
data.reduce((acc, obj) => {\r\n acc[obj.id] = obj;\r\n return acc;\r\n}, {});
:
This method uses the reduce()
function to iterate over the array and create an object with the IDs as keys and values.Pros: flexible and can be used for other types of reductions.
Cons: might have performance overhead due to the use of reduce()
and accessing object properties.
data.reduce((acc, obj) => ({\r\n ...acc,\r\n [obj.id]: obj\r\n}), {});
:
This method is similar to the previous one but uses an object literal with the ID as a key and the value as the property.Pros: concise and readable code.
Cons: might have performance overhead due to the use of reduce()
and creating object literals.
new Map(data.map(obj => [obj.id, obj]));
:
This method creates a new Map
object from an array of key-value pairs. The map()
function is used to extract the objects with their IDs as keys and values.Pros: efficient use of memory and can be faster than other approaches. Cons: might not be suitable for all use cases, such as when iterating over the map.
const res = {}; data.forEach(obj => {\r\n\tres[obj.id] = obj; \r\n});
:
This method uses a simple loop to iterate over the array and create an object with the IDs as keys and values.Pros: straightforward and easy to understand.
Cons: might have performance overhead due to the use of forEach()
and accessing object properties.
const res = {};\r\ndata.forEach(obj => {\r\n\tres[obj.id] = obj; \r\n});
,
This method is similar to the previous one but uses a more concise syntax with template literals.Pros: concise and readable code.
Cons: might have performance overhead due to the use of forEach()
and accessing object properties.
const res = {}; data.forEach(obj => {\r\n\tres[obj.id] = obj; \r\n});
,
This method is similar to the previous one but uses a more modern syntax with destructuring.Pros: concise and readable code.
Cons: might have performance overhead due to the use of forEach()
and accessing object properties.
const res = {}; data.forEach(obj => {\r\n\tres[obj.id] = obj; \r\n});
,
This method is similar to the previous one but uses a more functional programming style with arrow functions.Pros: concise and readable code.
Cons: might have performance overhead due to the use of forEach()
and accessing object properties.
In terms of performance, the results show that:
Object.fromEntries()
and Map
objects are the fastest approaches, followed closely by reduce()
methods.forEach()
is slower than the other approaches, especially for large datasets.Overall, the choice of approach depends on the specific use case and personal preference. However, if performance is critical, using Map
objects or reduce()
methods might be a better option.