Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs map + fromEntries + reduce spread
(version: 0)
Comparing performance of:
reduce vs map + fromEntries vs reduce spread
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(100).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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
147629.2 Ops/sec
map + fromEntries
76053.2 Ops/sec
reduce spread
70411.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The test compares three JavaScript approaches for iterating over an object and converting its values to strings: 1. `reduce()` 2. `map()` + `fromEntries()` 3. `reduce spread` (using the spread operator `...`) These approaches are designed to measure their performance, execution frequency, and memory usage. **Options Compared** Here's a brief overview of each approach: * **Reduce()**: Iterates over an array of key-value pairs in an object, accumulating the values into an accumulator object. In this case, it converts each value to a string using `toString()` and returns the final resulting object. * **Map() + fromEntries()**: First, maps each key-value pair in the object to a new array of arrays (using `map()`), where each inner array contains the key and its corresponding value. Then, it uses `fromEntries()` to convert this array into an object with the same structure as the original input. * **Reduce spread**: Similar to `reduce()`, but uses the spread operator `...` to initialize the accumulator object and merge it with new properties in each iteration. **Pros and Cons** Here are some pros and cons of each approach: * **Reduce()**: + Pros: Simple, straightforward implementation. Works well for small to medium-sized objects. + Cons: Can be slower than other approaches for large objects due to the overhead of recursive function calls. * **Map() + fromEntries()**: + Pros: Efficient for large objects, as it creates a new array and object that don't modify the original data. Can be faster than `reduce()` for very large inputs. + Cons: Requires an additional step (creating the inner array) and uses more memory to store the temporary array. * **Reduce spread**: + Pros: Similar to `reduce()`, but with less overhead due to the use of the spread operator. Can be faster than `reduce()` for large objects. + Cons: Less intuitive than `reduce()` or `map() + fromEntries()`, as it uses a different syntax. **Library/Feature Mention** In this benchmark, there is no explicit mention of any JavaScript libraries or special features being used. However, the use of `fromEntries()` implies that the implementation targets modern JavaScript versions (ECMAScript 2015+). **Other Considerations** When evaluating performance, it's essential to consider factors such as: * Cache locality: The test data is created using an array of keys and values, which can lead to cache locality issues for large inputs. * Memory allocation: Each approach allocates memory differently (e.g., `reduce()` creates a new object, while `map() + fromEntries()` uses more memory for the temporary array). * Browser-specific optimizations: The benchmark runs in Chrome 114 on Mac OS X 10.15.7, which may have specific optimizations or quirks that affect performance. **Alternative Approaches** Other approaches to iterate over an object and convert its values to strings might include: * Using a `for...in` loop * Employing a library like Lodash or Ramda for functional programming * Utilizing a more modern JavaScript feature, such as `Object.entries()` with `forEach()`
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object spread vs New map with string keys
Map convert
push vs spread (reduce array)
Comments
Confirm delete:
Do you really want to delete benchmark?