Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs Map edited
(version: 0)
Comparing performance of:
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:
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]) => { return {...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 (3)
Previous results
Fork
Test case name
Result
Reduce (reuse object)
Reduce (creating temporary objects)
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
19 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce (reuse object)
3399.6 Ops/sec
Reduce (creating temporary objects)
153.7 Ops/sec
Map
1242.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is tested?** The provided JSON represents a JavaScript microbenchmark that compares three approaches to transform an object created from an array of keys and values. The three approaches are: 1. **Object.fromEntries**: A modern JavaScript method introduced in ECMAScript 2015 (ES6) that creates an object from an array of key-value pairs. 2. **Reduce with creating temporary objects**: This approach uses the `reduce()` method to iterate over the array of keys and values, creating a new object by concatenating or formatting string representations of each value in memory. 3. **Map**: A built-in JavaScript data structure that can be used as an object, allowing you to store key-value pairs. **Comparison** The test cases compare these three approaches: * **Reduce (reuse object)**: Creates a new object and iterates over the array of keys and values using `reduce()`. It creates temporary objects for each value. * **Reduce (creating temporary objects)**: Similar to the previous approach but concatenates or formats string representations of each value directly in memory without creating intermediate objects. * **Map**: Uses a `Map` data structure to store key-value pairs and iterates over the array using its `forEach()` method. **Pros and Cons** Here's an overview of each approach: 1. **Object.fromEntries**: * Pros: Efficient, readable, and modern way to create objects from arrays. * Cons: Requires JavaScript version that supports ES6 features (ECMAScript 2015 or later). 2. **Reduce with creating temporary objects**: * Pros: Works in older JavaScript versions that don't support `Object.fromEntries`. Can be efficient for simple transformations. * Cons: May lead to memory issues if the array of values is large, as it requires creating and storing intermediate objects in memory. 3. **Map**: * Pros: More flexible than traditional object creation methods since you can use a `Map` data structure to store key-value pairs and easily iterate over them using various methods like `forEach()` or `entries()`. * Cons: May be less readable for complex transformations compared to other approaches. **Special JavaScript features** This benchmark doesn't explicitly require any special JavaScript features beyond ES6 support for `Object.fromEntries`. However, it does utilize modern object creation and manipulation methods. **Alternative approaches** Here are some alternative approaches you might consider when transforming objects from arrays: * Using the spread operator (`{ ... }`) or the `Array.prototype.map()` method to create a new object based on an array of key-value pairs. * Leveraging libraries like Lodash, which provides utility functions for working with data structures. Keep in mind that these alternatives might not offer performance benefits comparable to the approaches tested in this 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?