Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs Map with small objects
(version: 0)
Test with small objects
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) vs Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(5).keys()) };
Tests:
Object.fromEntries
Object.fromEntries(Object.entries(data).map((key, value) => [key, value]));
Reduce (reuse object)
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
Reduce (creating temporary objects)
Object.entries(data).reduce((acc, [k, v]) => ({ ...acc, [k]: v.toString() }), {});
Map
Object.entries(data).reduce((acc, [k, v]) => { acc.set(k, v.toString()) return acc; }, new Map());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
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 benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares four different approaches to create an object from a list of key-value pairs: 1. `Object.fromEntries` 2. `Array.prototype.reduce` with reuse of the accumulator object 3. `Array.prototype.reduce` with creation of temporary objects 4. `Map` The test case uses small objects, and the benchmark aims to determine which approach is the fastest. **Options Compared** Here's a brief overview of each option: 1. **Object.fromEntries**: This method was introduced in ECMAScript 2015 (ES6). It creates an object from an array of key-value pairs. 2. **Reduce (reuse object)**: In this approach, the accumulator object is reused throughout the reduction process. The `reduce` method is called with an initial value of `{}`, and each iteration modifies this object by adding new properties. 3. **Reduce (creating temporary objects)**: Similar to the previous approach, but a new object is created in each iteration to store the accumulated values. 4. **Map**: A `Map` is used as the accumulator object. This approach uses the `Map`'s `set` method to add key-value pairs. **Pros and Cons** Here's a brief analysis of each option: 1. **Object.fromEntries**: * Pros: Efficient, concise, and easy to read. * Cons: May not work as expected if the input array has duplicate keys (although this is mitigated by using `Object.fromEntries` with a `Map`-like object as its second argument). 2. **Reduce (reuse object)**: * Pros: Reuses the accumulator object, reducing memory allocation and deallocation overhead. * Cons: May lead to slower performance due to the reuse of an object that grows in size on each iteration. 3. **Reduce (creating temporary objects)**: * Pros: Creates a new object on each iteration, which can be faster than reusing an existing object. * Cons: Leads to more memory allocations and deallocations, which can slow down performance. 4. **Map**: * Pros: Efficient use of `Map`'s built-in features, such as fast lookups and insertions. * Cons: May have a higher overhead due to the creation of an additional object. **Special JS Feature/Syntax** None mentioned in this benchmark. **Library Usage** The `Array.prototype.reduce` method is used extensively throughout the benchmark. The `Map` constructor is also used, but not as part of a library or framework. **Other Alternatives** Some alternative approaches to creating objects from key-value pairs include: 1. **Using `Object.assign()`**: This method can be used to create an object by merging multiple objects together. 2. **Using a `for...of` loop and string interpolation**: This approach involves using a `for...of` loop to iterate over the key-value pairs and creating a new object using string interpolation. These alternatives are not explored in this benchmark, but they may be worth considering depending on specific use cases. I hope this explanation helps!
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 Map
Comments
Confirm delete:
Do you really want to delete benchmark?