Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Convert Array to Object - Compare all possible5
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) vs Map vs forEach vs for of vs for i vs for i saved length variable
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys()).map(i => ({id: i, value: `val_${i}`}));
Tests:
Object.fromEntries
Object.fromEntries(data.map(obj => [obj.id, obj]));
Reduce (reuse object)
data.reduce((acc, obj) => { acc[obj.id] = obj; return acc; }, {});
Reduce (creating temporary objects)
data.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {});
Map
new Map(data.map(obj => [obj.id, obj]));
forEach
const res = {}; data.forEach(obj => { res[obj.id] = obj; });
for of
const res = {}; for (const obj of data) { res[obj.id] = obj; }
for i
const res = {}; for (let i = 0; i < data.length; i++) { res[data[i].id] = data[i]; }
for i saved length variable
const res = {}; const length = data.length; for (let i = 0; i < length; i++) { res[data[i].id] = data[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (8)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
Map
forEach
for of
for i
for i saved length variable
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `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. 2. `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. 3. `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. 4. `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. 5. `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. 6. `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. 7. `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. 8. `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. * The simple loop with `forEach()` is slower than the other approaches, especially for large datasets. * The more concise syntaxes with template literals or destructuring do not provide a significant performance boost. 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.
Related benchmarks:
Array range generating
Object.fromEntries w/Array.map vs Array.reduce
Data normalization
Map vs Object with Number Keys
Map convert
Comments
Confirm delete:
Do you really want to delete benchmark?