Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce v21
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object)
Created:
3 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]) => [`a_${key}`, value]))
Reduce (reuse object)
Object.entries(data).reduce((acc, [k, v]) => { acc[`a_${k}`] = v; return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
472.9 Ops/sec
Reduce (reuse object)
771.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two approaches to create an object from key-value pairs in JavaScript: 1. `Object.fromEntries` 2. Using the `reduce` method on the `entries` method of an object **Library Usage: None** There is no explicit library used in this benchmark. **Special JavaScript Features or Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Benchmark Test Cases** The benchmark has two test cases: 1. **Object.fromEntries** * This test case creates an object from key-value pairs using the `Object.fromEntries` method. * The input data is generated using the `Array.from` and `Array.keys()` methods, creating an array of 10,000 objects with unique keys. 2. **Reduce (Reuse Object)** * This test case uses the `reduce` method to create a new object from key-value pairs on the same array of objects used in the first test case. * The `reduce` callback function iterates through the array and adds each key-value pair to a new object, reusing an existing object to avoid creating a new one for each iteration. **Comparison** The benchmark compares the performance of these two approaches: 1. **Object.fromEntries**: Creates a new object from key-value pairs using the `fromEntries` method. 2. **Reduce (Reuse Object)**: Uses the `reduce` method on the `entries` method of an object to create a new object from key-value pairs, reusing an existing object. **Pros and Cons** 1. **Object.fromEntries**: * Pros: + Faster and more concise way to create objects from key-value pairs. + Less boilerplate code required. * Cons: + Creates a new object each time it's used, which can be inefficient for large datasets. 2. **Reduce (Reuse Object)**: * Pros: + Reuses an existing object, reducing memory allocation and deallocation overhead. + Can be more efficient for large datasets. * Cons: + Requires the use of the `reduce` method, which can add complexity to code. + May require additional manual management of object references. **Alternative Approaches** Other approaches to create objects from key-value pairs in JavaScript include: 1. **Object.assign()**: Assigns properties from an array of key-value pairs to a target object. 2. **forEach()**: Iterates through an array and adds each key-value pair to a target object using the `forEach` method. However, these approaches may not offer the same level of performance or conciseness as `Object.fromEntries` or the `reduce` method. I hope this explanation helps!
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
Comments
Confirm delete:
Do you really want to delete benchmark?