Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of Objects to Object with keys Object.fromEntries v Object.assign
(version: 0)
Convert an array of objects to an object whose keys are derrived from a property on each object in array.
Comparing performance of:
Object.fromEntries vs Object.assign
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function createArray(propsCount) { const arr = [] for (let i = 0; i < propsCount; i += 1) { arr.push({ id: i, value: i + ' value' }) } return arr } var data = createArray(100)
Tests:
Object.fromEntries
const obj = Object.fromEntries(data.map(item => [item.id, item.value]));
Object.assign
const obj = Object.assign({}, ...data.map((item) => ({[item.id]: item.value})));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
Object.assign
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 break down the benchmark and its components. **Benchmark Definition** The benchmark is designed to compare two ways of converting an array of objects into an object using JavaScript's `Object.fromEntries` and `Object.assign`. The conversion involves mapping each object in the array, transforming it into a key-value pair, and then creating an object from these pairs. This process is repeated 100 times (due to the use of the `createArray(100)` function) for performance comparison. **Options Compared** Two approaches are compared: 1. **Object.fromEntries**: A modern JavaScript method introduced in ECMAScript 2017 that creates an object by passing an iterable (in this case, an array map) as its key-value pairs. 2. **Object.assign**: An older JavaScript method that merges all provided objects into a single object, using the spread operator (`...`). **Pros and Cons of Each Approach** 1. **Object.fromEntries**: * Pros: Generally faster and more concise than `Object.assign`. It's also more expressive and readable. * Cons: Not supported in older browsers (e.g., Internet Explorer). May not work as expected if the input array is not iterable or contains non-key-value pairs. 2. **Object.assign**: * Pros: Widely supported across browsers, including older ones. Can be used with a single object and an array of objects to merge into it. * Cons: Less concise and less expressive than `Object.fromEntries`. May require additional steps for more complex conversions. **Library Used** None explicitly mentioned in the benchmark definition; however, both methods rely on built-in JavaScript functionality. **Special JS Feature/Syntax (None)** No special features or syntax are used beyond standard JavaScript. **Other Considerations** The benchmark is designed to measure performance differences between `Object.fromEntries` and `Object.assign`, which are widely used methods in modern web development. This comparison can help developers decide which approach to use based on their specific needs, browser support requirements, and personal preference. **Alternatives** For this particular conversion task, other approaches could include: 1. Using a library like Lodash (`_.mapValues`) or Ramda (`R.mapKeys`), which provide more concise and expressive ways to perform similar conversions. 2. Implementing a custom solution using a loop or recursion for the key-value pair transformation. 3. Utilizing newer JavaScript features, such as `Map` objects (e.g., creating an object from key-value pairs using `map.entries()`). However, these alternatives are not specifically tested in this benchmark.
Related benchmarks:
Map convert
fromEntries vs reduce 2
Object.entries vs Generator
For in vs Object.*.forEach vs Object.values 2
Comments
Confirm delete:
Do you really want to delete benchmark?