Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
List iteration performance test
(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: 200 }, (_, 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
1006188.9 Ops/sec
Map
1108487.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of two different approaches: `Map` and `Reduce`. Both approaches are used to transform the input data (`data`) into a new array with specific properties. The main difference between the two lies in how the transformation is applied to each element of the data. **What's being tested?** The test cases compare the performance of the following operations: 1. **Map**: Create a new array by applying a callback function to each element of `data`. This function extracts specific properties from each object and returns an object with those properties. 2. **Reduce**: Apply a callback function to each element of `data`, accumulating the results in an accumulator array. **Options compared** The two options being compared are: 1. **Map**: A concise way to transform data into a new array by applying a function to each element. 2. **Reduce**: An iterative approach that accumulates the results of applying a function to each element. **Pros and Cons:** * **Map**: + Pros: - Concise syntax - Easy to read and maintain - Suitable for simple transformations + Cons: - Can be slower than `Reduce` due to the overhead of creating a new array - May not be suitable for complex transformations or large datasets * **Reduce**: + Pros: - More flexible and powerful than `Map` - Suitable for complex transformations and large datasets - Can be more efficient in terms of memory usage + Cons: - More verbose syntax - Requires manual handling of the accumulator array **Library:** The `Array.prototype.map()` method is used in one of the test cases. This method applies a given function to each element in an array, returning a new array with the results. The `Array.prototype.reduce()` method is also used in another test case. This method applies a given function to each element in an array, accumulating the results in an accumulator array. **Special JS features:** None of the test cases use any special JavaScript features or syntax that are not widely supported. **Alternative approaches:** Other alternatives for transforming data include: 1. **Loops**: Using traditional loops (e.g., `for` loops) to iterate over the data and accumulate the results. 2. **Arrow functions**: Using arrow functions to simplify the transformation code. 3. **Modern ES6+ features**: Leveraging modern ES6+ features, such as `flatMap()`, `forEach()`, or `reduce()` with an initial value. Keep in mind that these alternatives may not be suitable for all use cases and may have different performance characteristics compared to `Map` and `Reduce`.
Related benchmarks:
map vs forEach vs for loop
Math.pow() versus **
javascript loops (foreach, for-in, for-of, for, map, reduce) 1000 items
javascript loops with reduce 3
List iteration performance test 1
Comments
Confirm delete:
Do you really want to delete benchmark?