Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs Map constructor
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) vs Map
Created:
2 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 (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
new Map(Object.entries(data)).forEach((val, k, map) => { map.set(k, val.toString()) });
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 dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three ways to create an object from key-value pairs: 1. `Object.fromEntries()` 2. Using the `reduce()` method to create an object 3. Using a `Map` constructor and then iterating over it to set values **Test Case Explanation** Each test case measures the execution time of a specific approach. 1. `Object.fromEntries`: This approach uses the `fromEntries()` method, which creates an object from an array of key-value pairs. 2. "Reduce (reuse object)": This test case uses the `reduce()` method to create an object. The `"reuse object"` part suggests that this implementation reuses the accumulator object (`acc`) instead of creating a new one for each iteration. 3. "Reduce (creating temporary objects)": This test case also uses the `reduce()` method, but creates a new object for each iteration using the `{...acc}` syntax. 4. "Map": This approach uses a `Map` constructor and then iterates over it to set values in an object. **Library Usage** * The `Object.fromEntries()` method is used in the first test case. It's a built-in JavaScript method that creates an object from an array of key-value pairs. * The `reduce()` method is used in two test cases, "Reduce (reuse object)" and "Reduce (creating temporary objects)". This is a built-in JavaScript method that applies a function to each element of an array. **Special JS Feature/Syntax** None of the tested approaches use any special JavaScript features or syntax beyond what's described above. **Options Comparison** Here's a summary of the pros and cons of each approach: * `Object.fromEntries()`: Fast and efficient, as it creates an object directly from key-value pairs. * "Reduce (reuse object)": Reuses the accumulator object, which can be faster for large datasets but may lead to memory issues if not handled carefully. * "Reduce (creating temporary objects)": Creates a new object on each iteration, which can be slower and less efficient than other approaches. * "Map": Uses a `Map` constructor and then iterates over it, which can be an interesting approach but may not be as fast or efficient as other methods. **Alternative Approaches** Other possible ways to create an object from key-value pairs include: * Using the spread operator (`{...data}`) and iterating over the keys * Using a `for...in` loop to iterate over the keys and set values * Using a library like Lodash's `pick()` function These alternatives may have different performance characteristics, memory usage, or readability compared to the tested approaches.
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?