Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Max and Min values
(version: 0)
Comparing performance of:
Reduce vs For
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [] for (var x = 1; x <= 100000; x++) { data.push({ x: x, y: Math.floor(Math.random() * (1000000)) }) }
Tests:
Reduce
function getMinY() { return data.reduce((min, p) => p.y < min ? p.y : min, data[0].y); } function getMaxY() { return data.reduce((max, p) => p.y > max ? p.y : max, data[0].y); } const min = getMinY(); const max = getMaxY();
For
function getMinY(){ var minY = data[0]; for (var i = 1; i < data.length; i++) { if (data[i] < minY) { minY = data[i]; } } return minY; } function getMaxY(){ var maxY = data[0]; for (var i = 1; i < data.length; i++) { if (data[i] > maxY) { maxY = data[i]; } } return maxY; } var min = getMinY(); var max = getMaxY();
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 provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The provided JSON defines a JavaScript microbenchmark that measures the performance of finding the minimum and maximum values in an array using two different approaches: `reduce` and `for`. **Approach 1: Reduce** In this approach, the `getMinY()` function uses the `Array.prototype.reduce()` method to find the minimum value. The `reduce()` method applies a callback function to each element of the array, accumulating a value that is returned as the result. Pros: * Concise and expressive code * Built-in JavaScript method, so no additional library or overhead Cons: * Can be slower than manual iteration due to the overhead of the `reduce()` method **Approach 2: For** In this approach, the `getMinY()` function uses a traditional `for` loop to iterate through the array and find the minimum value. Pros: * Can be faster than `reduce()` for small arrays or when performance is critical * Allows for more control over iteration order Cons: * Longer and more verbose code * Requires manual memory management (in this case, storing intermediate results) **Library/Functionality Used** The `Array.prototype.reduce()` method is a built-in JavaScript function that belongs to the Array prototype. It's used in both test cases. **Special JS Feature/Syntax** None are mentioned or required for these benchmark tests. **Benchmark Preparation Code** The script preparation code generates an array of 100,000 random numbers between 1 and 1,000,000 using a `for` loop. This creates a large dataset that's used in both test cases to measure performance. **Test Cases** There are two individual test cases: 1. "Reduce" 2. "For" Both test cases use the same script preparation code to generate an array of random numbers and then find the minimum and maximum values using their respective approaches. **Benchmark Results** The latest benchmark results show that Chrome 70 on a Desktop Mac running Mac OS X 10.14.0 outperforms itself with an execution rate of approximately 189 executions per second for the "Reduce" test case, while taking around 6 executions per second for the "For" test case. **Alternatives** Other approaches to finding minimum and maximum values in an array include: * Using `Math.min()` and `Math.max()` functions (similar to Approach 2's `for` loop) * Using a manual iterative approach with index-based access to elements (not shown in this benchmark) Keep in mind that the choice of approach depends on the specific requirements of your use case, including performance considerations, code readability, and maintainability.
Related benchmarks:
Max and Min values
Max and Min values
Max and Min values
Labels
Comments
Confirm delete:
Do you really want to delete benchmark?