Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs. ForEach in 2D array
(version: 0)
Finding the last non-null index in a 2D array can be done in several ways, two of them are using a ForEach vs. a Reduce method.
Comparing performance of:
ForEach vs Reduce
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ [1, 1, null, null, null, null], [1, null, null, 1, 1, null], [null, null, 1, 1, null, null, null], [1, 1, 1, 1, null, null] ];
Tests:
ForEach
const rightMostPosition = {x: 0, y: 0}; array.forEach((element, yIndex) => { const xIndex = element.reduceRight( (result, current, index) => result ?? (current && index) ); if (xIndex > rightMostPosition.x) { rightMostPosition.x = xIndex; rightMostPosition.y = yIndex; } });
Reduce
const rightMostPosition = array.reduce((position, currentArray, yIndex) => { const xIndex = currentArray.reduceRight( (result, currentBlock, index) => result ?? (currentBlock && index) ); if (position.x < xIndex) { position.x = xIndex; position.y = yIndex; } return position; }, {x: 0, y: 0});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ForEach
Reduce
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 break down the provided benchmark definition and test cases. **Benchmark Description** The benchmark is designed to compare two approaches for finding the last non-null index in a 2D array: using `forEach` and `reduce`. **Script Preparation Code** The script preparation code defines a 4x6 2D array, which will be used as input for both test cases. ```javascript var array = [ [1, 1, null, null, null, null], [1, null, null, 1, 1, null], [null, null, 1, 1, null, null, null], [1, 1, 1, 1, null, null] ]; ``` **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** The benchmark includes two test cases: ### ForEach ```javascript const rightMostPosition = {x: 0, y: 0}; array.forEach((element, yIndex) => { const xIndex = element.reduceRight( (result, current, index) => result ?? (current && index) ); if (xIndex > rightMostPosition.x) { rightMostPosition.x = xIndex; rightMostPosition.y = yIndex; } }); ``` This test case uses the `forEach` method to iterate over the 2D array, and for each element, it uses the `reduceRight` method to find the last non-null index in the element's row. The result is stored in the `rightMostPosition` object. ### Reduce ```javascript const rightMostPosition = array.reduce((position, currentArray, yIndex) => { const xIndex = currentArray.reduceRight( (result, currentBlock, index) => result ?? (currentBlock && index) ); if (position.x < xIndex) { position.x = xIndex; position.y = yIndex; } return position; }, {x: 0, y: 0}); ``` This test case uses the `reduce` method to iterate over the 2D array and find the last non-null index. The `reduceRight` method is used on each row of the array to find the last non-null element. **Library Usage** Both test cases use the following libraries: * `Array.prototype.forEach`: a built-in JavaScript function for iterating over arrays. * `Array.prototype.reduce`: a built-in JavaScript function for reducing an array to a single value. The `reduceRight` method is not a standard JavaScript method, but it can be implemented using the `Array.prototype.reduce` method with some modifications (not shown in this example). **Special JS Features/Syntax** Neither test case uses any special JavaScript features or syntax. They only rely on built-in methods and basic JavaScript operations. **Alternatives** If you wanted to implement a similar benchmark, you could use other approaches such as: * Using `map` and `Math.max` to find the last non-null index in each row. * Using `every` and `filter` to find the last non-null element in each row. * Implementing your own loop and logic to find the last non-null index. Keep in mind that these alternatives may have different performance characteristics compared to using built-in methods like `forEach` and `reduce`.
Related benchmarks:
Tim's reduce vs flatMap
flatMap vs reduces
flatMap vs reduce small array
flatMap vs reduce, but without copying the array in each iteration
flatMap vs reduce flattern array
Comments
Confirm delete:
Do you really want to delete benchmark?