Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sorting array into 2 arrays with reduce vs forEach
(version: 0)
When sorting an array, you can use reduce or forEach
Comparing performance of:
reduce vs forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var timeSlots = [ {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 0}, {start_date: "2020-10-15T19:14:46.599Z", end_date: "2020-10-15T19:14:46.599Z", id: 1}, ];
Tests:
reduce
var [ newTimeSlots, editedTimeSlots ] = timeSlots.reduce( (splitSlots, slot ) => { var arrayIndex = slot.id > 0 ? 1 : 0; splitSlots[arrayIndex].push({...slot, meeting: 1}); return splitSlots; }, [[], []]);
forEach
var newTimeSlots = []; var editedTimeSlots = []; timeSlots.forEach(slot => { slot.id > 0 ? editedTimeSlots.push({...slot, meeting: 1}) : newTimeSlots.push({...slot, meeting: 1}); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
forEach
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):
Let's dive into the benchmark. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that tests two approaches to sorting an array of objects with a specific structure. The test case compares the performance of using `reduce` vs `forEach` methods to split an array of time slots into two separate arrays, each containing only the time slots with an ID greater than 0. **Options being compared** The benchmark tests two options: 1. **Reduce**: Using the `reduce` method to split the array. This approach is often used for functional programming and can be more concise. 2. **ForEach**: Using the `forEach` method to iterate over the array and split it into two separate arrays. **Pros and cons of each approach** * **Reduce**: + Pros: Can be more concise and expressive, especially when dealing with complex data structures. + Cons: May have performance overhead due to the creation of a new accumulator object for each iteration. * **ForEach**: + Pros: Can be easier to understand and maintain, as it's a familiar loop construct. + Cons: May require more code and be less concise than using `reduce`. **Library usage** There is no explicit library mentioned in the benchmark definition or test cases. However, it's worth noting that some JavaScript environments (e.g., browsers) may have built-in optimizations or caching for certain functions, which could impact the results. **Special JS feature** The benchmark uses a specific syntax for object destructuring and spreading (`{...slot, meeting: 1}`), which is supported by modern JavaScript engines. This syntax allows for concise creation of new objects with modified properties. **Other alternatives** If you wanted to test other approaches, here are some additional options: * **Map**: Similar to `reduce`, but uses the `map` method instead. * **Destructuring assignment with multiple loops**: Using multiple loops to assign values from each object to separate arrays. * **Looping constructs**: Using traditional `for` loops or `while` loops to iterate over the array and split it into two arrays. Keep in mind that these alternatives might not be as concise or expressive as using `reduce` or `forEach`, but can still provide interesting performance comparisons.
Related benchmarks:
_.sortBy vs native sort
simple stuff
iso date string sort comparison
ISO 8601 parsing
Comments
Confirm delete:
Do you really want to delete benchmark?