Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs map + fromEntries vs reduce spread
(version: 0)
Comparing performance of:
reduce vs map + fromEntries vs reduce spread
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
reduce
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
map + fromEntries
Object.fromEntries(Object.entries(data).map(([k, v]) => ([k, v.toString()])));
reduce spread
Object.entries(data).reduce((acc, [k, v]) => ({ ...acc, [k]: v.toString(), }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
map + fromEntries
reduce spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
2326.5 Ops/sec
map + fromEntries
2103.9 Ops/sec
reduce spread
0.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test and its components. **What is tested on the provided JSON?** The benchmark tests three different approaches to achieve the same result: 1. `Object.entries(data).reduce((acc, [k, v]) => {\r\n acc[k] = v.toString();\r\n return acc;\r\n}, {});` 2. `Object.fromEntries(Object.entries(data).map(([k, v]) => ([k, v.toString()]));)` 3. `Object.entries(data).reduce((acc, [k, v]) => ({\r\n ...acc,\r\n [k]: v.toString(),\r\n}), {});` These approaches aim to create a new object by iterating over the key-value pairs of an existing object (`data`) and converting each value to a string. **Options compared:** The three options are being compared: * `reduce`: A method that iterates over an array (or other iterable) and applies a callback function to each element. The accumulator is updated with each iteration. * `map + fromEntries`: This approach uses the `map` method to transform each value in `data` into a new array of key-value pairs, which is then passed to `Object.fromEntries`. * `reduce spread`: A variation of the `reduce` method that spreads the accumulator (`acc`) as an object, allowing for more flexibility when assigning values. **Pros and cons:** Here's a brief summary of each approach: * **Reduce**: + Pros: Efficient use of memory, easy to implement. + Cons: Can be slow if not implemented correctly, may not work well with large datasets. * **Map + fromEntries**: + Pros: Fast and efficient, works well with large datasets. + Cons: May consume more memory due to the creation of an intermediate array, less intuitive than `reduce`. * **Reduce spread**: + Pros: Offers flexibility in assigning values to the accumulator, can be more readable. + Cons: May not work as expected if the callback function is complex, may require additional considerations for edge cases. **Library and purpose:** There are no libraries mentioned in this benchmark. However, `Object.entries()` and `Object.fromEntries()` are part of the ECMAScript standard (ES6+) and are built-in methods for working with objects in JavaScript. **Special JS feature or syntax:** The use of spread syntax (`...`) is a modern JavaScript feature introduced in ES6+. It allows for the expansion of an object's properties into individual key-value pairs, making it easier to merge objects or create new objects from existing ones.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object spread vs New map with string keys
Object.fromEntries on array vs reduce on array
push vs spread (reduce array)
Comments
Confirm delete:
Do you really want to delete benchmark?