Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce object.assign
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (object assign) vs Reduce (creating temporary objects)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
Object.fromEntries
Object.fromEntries(Object.entries(data).map((key, value) => [key, value]));
Reduce (object assign)
Object.entries(data).reduce((object, [key, value]) => Object.assign(object, { [key]: value }), {})
Reduce (creating temporary objects)
Object.entries(data).reduce((acc, [k, v]) => ({ ...acc, [k]: v.toString() }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (object assign)
Reduce (creating temporary objects)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
182.6 Ops/sec
Reduce (object assign)
197.2 Ops/sec
Reduce (creating temporary objects)
0.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the provided benchmark. **Benchmark Overview** The benchmark compares three approaches for creating an object from an array of key-value pairs: `Object.fromEntries`, `reduce(object assign)`, and `reduce (creating temporary objects)`. **Options Compared** 1. **`Object.fromEntries`**: This method is a built-in JavaScript function introduced in ECMAScript 2015. It takes an iterable of key-value pairs as its argument and returns a new object. 2. **`Reduce (object assign)`**: This approach uses the `reduce()` method to iterate over the array of key-value pairs and creates a new object by assigning each value to the object using the spread operator (`Object.assign()`) and accumulator. 3. **`Reduce (creating temporary objects)`**: This approach is similar to the previous one, but instead of creating an empty object as the accumulator, it creates a new object with `toString()` on each property. **Pros and Cons** 1. **`Object.fromEntries`**: * Pros: Fast, efficient, and concise. * Cons: May not be supported in older browsers or versions of JavaScript (e.g., Internet Explorer 11). 2. **`Reduce (object assign)`**: * Pros: Wide browser support, easy to implement. * Cons: Can be slower due to the overhead of `Object.assign()`. 3. **`Reduce (creating temporary objects)`**: * Pros: May be faster than `Object.fromEntries` since it avoids the creation of a new object for each iteration. * Cons: Creates an extra object on each iteration, which can lead to performance issues in some cases. **Library and Special JS Features** There are no libraries used in this benchmark. However, note that `Object.fromEntries()` is a built-in JavaScript function introduced in ECMAScript 2015. No special JavaScript features or syntax are required for this benchmark. **Alternatives** If you're looking for alternative approaches to create an object from an array of key-value pairs, you could consider: 1. Using the `Map` data structure: You can create a new `Map` instance and add entries using the `set()` method. 2. Using the `Array.prototype.reduce()` method with a custom callback function. 3. Using a library like Lodash or Moment.js (although these libraries are not necessary for this specific use case). Keep in mind that the performance differences between these approaches may be negligible, and it's essential to consider the trade-offs between code readability, maintainability, and performance when choosing an approach.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs create temp object vs Array.reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?