Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map -> fromEntries
(version: 1)
Comparing performance of:
Object.fromEntries() vs Reduce
Created:
8 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const inputMap = new Map() for (const key of Array.from(Array(1000).keys())) { inputMap.set(crypto.randomUUID(), [crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID(), crypto.randomUUID()].join('')) }
Tests:
Object.fromEntries()
Object.fromEntries(inputMap)
Reduce
inputMap.entries().reduce((acc, item) => { acc[item[0]] = item[1] return acc }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries()
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries()
17351.8 Ops/sec
Reduce
7826.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
The benchmark provided tests two different approaches for transforming a `Map` object into a plain JavaScript object. Specifically, the `Map` contains randomly generated entries, with each key associated with a string value made up of multiple UUIDs. ### Test Cases Compared 1. **Object.fromEntries(inputMap)** - **Description**: This method takes an iterable of key-value pairs (like a `Map`) and converts it directly into an object. - **Pros**: - Cleaner syntax and more readable code. - Typically more optimized for performance in modern JavaScript engines since it is a built-in method specifically designed for this purpose. - **Cons**: - Less flexibility compared to custom solutions if any additional manipulation is needed during conversion. 2. **inputMap.entries().reduce((acc, item) => { acc[item[0]] = item[1]; return acc; }, {})** - **Description**: This method uses the `reduce()` function to iterate over the entries of the `Map`, accumulating the results into a plain object. - **Pros**: - Offers the ability to easily modify the way data is accumulated, allowing for additional processing or transformation during conversion. - **Cons**: - Generally more verbose and less readable than `Object.fromEntries()`. - Typically slower due to the overhead of using `reduce()` and manual assignment of each key-value pair. ### Benchmark Results The benchmark results indicate the performance of each method measured in "executions per second": - **Object.fromEntries()**: 17351.84 executions per second - **Reduce**: 7826.72 executions per second From the results, it's clear that `Object.fromEntries()` performs significantly better than the `reduce()` approach. This highlights how modern JavaScript engines are optimized for built-in methods. ### Libraries and Features Used - **crypto.randomUUID()**: This is a native JavaScript method that generates a new UUID (Universally Unique Identifier). In this benchmark, it is used to create unique keys and random string values for the `Map`. It is a useful feature for scenarios where uniqueness is required, such as when generating identifiers. ### Alternatives and Considerations While this benchmark specifically tests these two methods, there are other potential approaches to converting a `Map` to an object: 1. **For-Of Loop**: You could iterate over the `Map` using a `for...of` loop, manually adding key-value pairs to an object. - **Pros**: Greater control over the logic during conversion; simple syntax. - **Cons**: More verbose and potentially less performant than built-in methods. ```javascript const obj = {}; for (const [key, value] of inputMap) { obj[key] = value; } ``` 2. **Array.from()/Object.assign**: Another alternative is to use `Array.from()` combined with `Object.assign()` to convert the `Map`. - **Pros**: More functional programming style; concise syntax. - **Cons**: Still not as performant as `Object.fromEntries()`. ```javascript const obj = Object.assign({}, Array.from(inputMap)); ``` ### Conclusion This benchmark demonstrates the performance differences between built-in methods and custom logic for converting a `Map` into an object in JavaScript. While `Object.fromEntries()` offers superior performance and readability, the choice of method may also depend on the specific context and requirements of the task at hand, particularly if transformation logic is necessary. Moreover, understanding the performance trade-offs can help developers make informed decisions regarding which approach to use in their applications.
Related benchmarks:
map-array-object-get-set
Map bench2
Iterate Map entries
Large Map vs Object 2
Write Object vs Map
map vs object - key access (string key)
Map -> Array
Map iteration + clearing
map iteration benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?