Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create a Dictionary with Object.fromEntries vs Map
(version: 0)
Comparing performance of:
Object.fromEntries vs Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ids = Array.from(Array(10000).keys()) var data = ids.map(item => ({ id: item, random: Math.random() }))
Tests:
Object.fromEntries
// Create the dictionary const dictionary = Object.fromEntries( data.map((item) => [ // Key item.id, // Item item, ]) ); // Utilize it, to test the performance of the dictionary itself as well. ids.map((id) => dictionary[id]);
Map
// Create the dictionary const dictionary = new Map( data.map((item) => [ // Key item.id, // Item item, ]) ); // Utilize it, to test the performance of the dictionary itself as well. ids.map((id) => dictionary.get(id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
Map
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 provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition shows two different approaches to create a dictionary (object) in JavaScript: 1. **Object.fromEntries**: This method is used to create an object from key-value pairs. In this case, it's being used to create a dictionary where each key is the `id` property of an object and the value is the entire object. 2. **Map**: A Map is a built-in JavaScript data structure that stores key-value pairs in a way that allows for efficient lookup, insertion, and deletion. In this case, it's being used to create a dictionary where each key is the `id` property of an object. **Options Compared** The two options are compared in terms of their performance, specifically in terms of how many executions per second (measured in "ExecutionsPerSecond") they produce on Firefox 105 running on Ubuntu Desktop. **Pros and Cons of Each Approach** 1. **Object.fromEntries**: * Pros: Can be more concise and readable when creating simple dictionaries. * Cons: May have performance issues for large datasets due to the overhead of object creation and iteration. 2. **Map**: * Pros: Offers faster lookup, insertion, and deletion times compared to objects. * Cons: Requires a bit more code and may be less readable for simple dictionary creations. **Library and Purpose** In this benchmark, no specific library is used beyond the standard JavaScript features mentioned above. However, it's worth noting that `Map` is a built-in JavaScript data structure, while `Object.fromEntries` is a newer method introduced in ECMAScript 2015 (ES6). **Special JS Feature or Syntax** The `Array.from()` method and template literals are used to create an array of objects with the desired properties. The `.map()` function is also used to apply this transformation to each element in the array. **Other Considerations** 1. **Garbage Collection**: JavaScript's garbage collection mechanism may impact performance, especially for large datasets. 2. **Caching**: Browser caching can affect benchmark results, as cached versions of the script might not be executed or updated frequently enough. **Alternatives** If you're looking for alternatives to this benchmark, you could consider: 1. Using other data structures like `Set` or `WeakMap`, which have different performance characteristics. 2. Comparing the performance of other dictionary creation methods, such as using `Object.create()` and then assigning properties. 3. Examining how these results compare to other languages or environments, like Node.js or TypeScript. Keep in mind that benchmarks can be sensitive to specific test cases, hardware, and software configurations.
Related benchmarks:
Object keys vs Array map v2
Object.fromEntries vs new Map vs Reduce
Different ways of creating a Dictionary
Map convert
Object.entries VS Object.keys with obj[key]
Comments
Confirm delete:
Do you really want to delete benchmark?