Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs reduce (find max value) i10000
(version: 0)
Comparing performance of:
reduce vs for
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = []; for (var i = 0; i < 10000; i++) { list.push([0, 0, Math.floor(Math.random() * 100000).toString()]); }
Tests:
reduce
const result = list.reduce((acc, [, , value]) => { const valueNum = Number(value); if (valueNum > acc) { return valueNum; } return acc; }, 0);
for
let result = 0; for (const [, , value] of list) { const valueNum = Number(value); if (valueNum > result) { result = valueNum; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
for
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 benchmark and its options. **Benchmark Overview** The benchmark compares the performance of two approaches to find the maximum value in an array: 1. **Reduce Method**: Uses the `Array.prototype.reduce()` method, which applies a callback function to each element in the array, accumulating a result. 2. **For Loop Method**: Uses a traditional for loop with an array iteration. **Script Preparation Code** The script preparation code creates an array of 10,000 elements, where each element is an object containing three properties: `[0, 0, string value]`. The string values are generated using `Math.random()` and then converted to strings using `toString()`. This ensures that the comparison logic only needs to access the numeric part of the value. **Html Preparation Code** There is no HTML preparation code provided, which means this benchmark focuses on JavaScript execution performance without considering DOM-related overhead. **Options Comparison** The two options being compared are: 1. **Reduce Method**: Uses the `Array.prototype.reduce()` method, which iterates over the array using a single iteration. 2. **For Loop Method**: Uses a traditional for loop with an array iteration, which requires multiple iterations. **Pros and Cons of Each Approach** **Reduce Method:** Pros: * Efficient use of memory, as only one element is stored in the accumulator. * Less chance of stack overflows due to deeper nesting. Cons: * May be slower due to the overhead of creating and managing an accumulator object. * Not suitable for very large arrays, as it may cause JavaScript engine optimizations to prioritize memory usage over performance. **For Loop Method:** Pros: * More straightforward and easy to understand for some developers. * Can be more efficient in certain cases, as the JavaScript engine can optimize loop unrolling or other techniques. Cons: * Requires multiple iterations, which can lead to stack overflow issues for large arrays. * May result in slower performance due to the overhead of creating and destroying temporary variables. **Library: Array.prototype.reduce()** The `Array.prototype.reduce()` method is a built-in JavaScript library that provides an efficient way to iterate over arrays. It applies a callback function to each element, accumulating a result. This library is widely supported across modern browsers and Node.js versions. **Special JS Feature/Syntax:** There are no special JavaScript features or syntax used in this benchmark. **Device Platform and Operating System** The benchmark results were run on an Android device using Opera Mobile 74, which provides insight into how different devices perform under this specific benchmark. **Other Alternatives** Some alternative approaches to finding the maximum value in an array could include: * Using `Math.max()` with a callback function: `Array.prototype.map().reduce(Math.max, -Infinity)` * Using `Array.prototype.forEach()` and iterating manually * Using a custom-built algorithm optimized for performance Keep in mind that these alternatives might not be directly comparable to the original Reduce method or For Loop approach, as they may have different performance characteristics.
Related benchmarks:
Array.reduce vs for loops vs Array.forEach
Array.reduce
Javascript: reduce VS for with Math.max
Finding the max value of a property in an array of objects
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?