Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Map
(version: 0)
Benchmark what is best performant
Comparing performance of:
Get the total of all items using the reduce vs Get the total of all items using map
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var listOfObjects = Array.from({length: 100}).map((_, i) => ({ id: i, quantity: Math.round((Math.random() * i) + 1), price: Number(((Math.random() * i) + 1).toFixed(2)) }));
Tests:
Get the total of all items using the reduce
listOfObjects.reduce((acc, item) => acc + (item.quantity * item.price), 0);
Get the total of all items using map
let total = 0; listOfObjects.map(item => total += (item.quantity * item.price));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Get the total of all items using the reduce
Get the total of all items using map
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):
I'd be happy to help explain what's being tested in this benchmark and the pros and cons of each approach. **Benchmark Overview** The benchmark compares the performance of two approaches to calculate the total price of a list of items: `reduce` and `map`. The input data is generated randomly, consisting of 100 objects with unique IDs, quantities between 1 and the ID, and prices between $1.01 and $101.00. **Approach 1: Reduce** The first approach uses the `reduce` method to calculate the total price. The `reduce` method applies a function to each element in an array, accumulating a result. In this case, the accumulator (`acc`) is initialized to 0, and for each item, it adds the product of the quantity and price. **Pros:** 1. Memory efficiency: Only one variable (`total`) needs to be stored. 2. Simplified code: The calculation can be expressed in a single line. **Cons:** 1. Iteration order: The `reduce` method iterates over the array from left to right, which might not be optimal for all data distributions. 2. Cache locality: The accumulator (`total`) needs to be accessed multiple times, which can lead to cache misses. **Approach 2: Map** The second approach uses the `map` method to calculate the total price. The `map` method creates a new array with the results of applying a function to each element in an original array. In this case, it calculates the product of quantity and price for each item and accumulates them. **Pros:** 1. Cache locality: Each item is processed individually, reducing cache misses. 2. Flexibility: The `map` method can be used with other data structures or algorithms. **Cons:** 1. Memory overhead: A new array needs to be created to store the results. 2. More complex code: Two lines are needed to calculate and accumulate the total price. **Library/Language Features Used** * JavaScript: No specific library is mentioned, but JavaScript's built-in `Array` methods (`reduce`, `map`) are used. * Regular expressions: Not used explicitly, but the random number generation uses mathematical operations (e.g., `(Math.random() * i) + 1`). **Special JavaScript Features/Syntax** None of the code snippets use any special JavaScript features or syntax. **Other Alternatives** If you needed to compare other approaches, some alternatives could be: * Using a custom loop instead of `reduce` and `map`. * Employing parallel processing techniques (e.g., using Web Workers) to calculate both totals simultaneously. * Utilizing more advanced data structures, such as an array of accumulators or a prefix sum data structure. Keep in mind that the performance difference between these approaches may vary depending on the specific use case, hardware, and data distribution.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
flatMap + reduce vs reduce + reduce
Math.max vs Array.reduce
Reduce vs flatMap performance
Flatmap vs reduce with objects
Comments
Confirm delete:
Do you really want to delete benchmark?