Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
List iteration performance test 1
(version: 0)
Comparing performance of:
Reduce vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [ ...Array.from({ length: 20000 }, (_, i) => ({ id: (20 + i).toString(), name: `Task ${20 + i}`, start_date: `2024-${String(Math.floor(Math.random() * 12) + 1).padStart(2, '0')}-01`, end_date: `2024-${String(Math.floor(Math.random() * 12) + 1).padStart(2, '0')}-28`, budgeted_hours: Math.floor(Math.random() * 100) + 1, status: "some status" })), ];
Tests:
Reduce
const result = data.reduce((acc, audit) => { acc.push({ id: audit.id, title: audit.name, startDate: audit.start_date, endDate: audit.end_date, budgetedHours: audit.budgeted_hours, status: audit.status, }); return acc; }, []);
Map
const result = data.map((audit, i) => { return { id: audit.id, title: audit.name, startDate: audit.start_date, endDate: audit.end_date, budgetedHours: audit.budgeted_hours, status: audit.status, } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 128 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
440.5 Ops/sec
Map
650.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The test measures the performance of two different approaches to transform an array of objects: `map` and `reduce`. The transformation involves extracting specific properties (`id`, `title`, `startDate`, `endDate`, `budgetedHours`, and `status`) from each object in the array. **Comparison Options** There are two comparison options: 1. **Map**: This approach uses the `map()` method to create a new array with transformed objects. 2. **Reduce**: This approach uses the `reduce()` method to accumulate an array of transformed objects. **Pros and Cons of Each Approach** **Map** Pros: * More straightforward and easier to understand for those familiar with functional programming concepts. * Can be more efficient in terms of memory usage, as it creates a new array without modifying the original one. Cons: * May incur overhead due to the creation of a new array. * Requires additional iteration over the array elements. **Reduce** Pros: * More flexible and powerful for complex transformations, as it allows for aggregation and iteration. * Can be more efficient in terms of memory usage, as it avoids creating an intermediate array. Cons: * May be less intuitive for those unfamiliar with functional programming concepts. * Requires a callback function to specify the transformation logic. **Library Used** The `map()` method uses the built-in Array.prototype.map() method, which is part of the ECMAScript standard. The `reduce()` method also uses the built-in Array.prototype.reduce() method. **Special JS Feature or Syntax** This benchmark does not rely on any specific JavaScript feature or syntax that's unique to modern browsers or versions of Node.js. It's a fundamental aspect of the language and is widely supported across different platforms. **Other Alternatives** If you wanted to implement this transformation in other ways, you could consider: * Using `forEach()` instead of `map()`: This would iterate over each element in the array but return no value. * Using `for...of` loops: This would provide a more traditional iterative approach without the need for `map()` or `reduce()`. * Using libraries like Lodash or Underscore.js, which offer various utility functions for transformations and aggregations. Keep in mind that these alternatives might have different performance characteristics, readability, and use cases compared to using built-in methods like `map()` and `reduce()`.
Related benchmarks:
map vs forEach vs for loop
for, for of, forEach, and reduce. Each with and without reduced variable re-declarations.
javascript loops (foreach, for-in, for-of, for, map, reduce) 1000 items
javascript loops with reduce 3
Comments
Confirm delete:
Do you really want to delete benchmark?