Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce test 4
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects)
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) => [!value ? `invalid-${key}` : key, value]));
Reduce (reuse object)
Object.entries(data).reduce((acc, [k, v]) => { acc[!v ? `invalid-${k}` : k] = v; return acc; }, {});
Reduce (creating temporary objects)
Object.entries(data).reduce((acc, [k, v]) => ({ ...acc, [!v ? `invalid-${k}` : k]: v }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
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 benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of three different approaches to create an object from a list of key-value pairs: 1. `Object.fromEntries` 2. Using `reduce` with reusing an existing object (`Reduce (reuse object)`) 3. Using `reduce` without creating temporary objects (`Reduce (creating temporary objects)`) **Options being compared** The three options are: * `Object.fromEntries`: a built-in JavaScript method that creates an object from an array of key-value pairs. * `Reduce (reuse object)`: uses the `reduce` method to create a new object by iteratively appending key-value pairs to an existing object. * `Reduce (creating temporary objects)`: similar to the previous option, but creates a new object for each iteration, resulting in more memory allocations. **Pros and Cons** 1. **Object.fromEntries**: * Pros: efficient, concise, and easy to read. * Cons: may not be supported by older browsers or JavaScript engines. 2. **Reduce (reuse object)**: * Pros: can avoid unnecessary object creations, reducing memory allocation overhead. * Cons: requires more complex code, and the reuse of objects might lead to unexpected behavior if not used carefully. 3. **Reduce (creating temporary objects)**: * Pros: easier to understand for developers familiar with `reduce`, as it uses a more traditional approach. * Cons: results in unnecessary object creations, which can impact performance. **Library usage** None of the benchmark options explicitly use any external libraries. **Special JS features or syntax** The benchmark does not utilize any special JavaScript features or syntax that require specific handling (e.g., async/await, Promises, etc.). **Other alternatives** Alternative approaches to create an object from a list of key-value pairs could include: * Using `Array.prototype.reduce()` directly on the array, without creating an intermediate object. * Utilizing libraries like Lodash or Underscore.js, which provide optimized implementations for this common use case. However, these alternative approaches are not explicitly tested in the provided benchmark.
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 reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?