Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs for of #1000
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) vs for of
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() }), {});
for of
const res = {}; for (const [k,v] of Object.entries(data)) { res[k] = v.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)
for of
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):
**What is being tested?** The provided benchmark tests the performance of three different approaches to create an object from an array of key-value pairs: 1. `Object.fromEntries`: a built-in JavaScript method that creates an object from an iterable (in this case, an array) of key-value pairs. 2. `Reduce` with an object as the accumulator: in this approach, the `reduce` method is used to create an object by iterating over the array and accumulating values into the accumulator object. 3. `Reduce` creating temporary objects: similar to the previous approach, but instead of using the accumulator object directly, a new temporary object is created at each iteration. **Options compared** The three approaches are being tested against each other for performance differences: * `Object.fromEntries`: a built-in method that creates an object from an iterable. * `Reduce (reuse object)`: uses the object as the accumulator and accumulates values into it directly. * `Reduce (creating temporary objects)`: creates a new temporary object at each iteration of the reduce function. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`Object.fromEntries`**: * Pros: simple, efficient, and well-documented. * Cons: may have performance overhead due to method call. 2. **`Reduce (reuse object)`**: * Pros: can be more efficient than creating a new object at each iteration. * Cons: requires using the accumulator object directly, which might lead to memory issues for large datasets. 3. **`Reduce (creating temporary objects)`**: * Pros: easy to understand and maintain. * Cons: creates multiple temporary objects at each iteration, leading to increased memory allocation and garbage collection overhead. **Library usage** None of the provided benchmark code uses external libraries. However, if you're interested in exploring other approaches, some popular JavaScript libraries for working with objects and arrays include: * Lodash: provides a `reduce` function that can be used as an alternative to the built-in method. * Ramda: offers a `mapValues` function that creates an object from an array of key-value pairs. **Special JS features or syntax** The benchmark doesn't explicitly use any special JavaScript features or syntax, but it does rely on: * `Array.from`: creates an array from an iterable. * `Object.entries`: returns an array of key-value pairs for an object. * `reduce`: a method that applies a function to each element of an array and accumulates the results. **Other alternatives** If you're looking for alternative approaches, here are some additional options: * Using `for...of` loop with `Object.assign()`: creates an object from an iterable by iterating over it using a `for...of` loop and assigning values to a new object. * Using `Array.prototype.map()` and `Object.assign()`: transforms the array into an object by mapping each element to a key-value pair and then merging the results into a new object. These alternatives can be used as inspiration or as part of further benchmarking to explore performance differences.
Related benchmarks:
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
Object.fromEntries VS multiple reduce from list of data
Comments
Confirm delete:
Do you really want to delete benchmark?