function shuffle(array) {
for (let i = array.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function getRandomArray(size) {
const arr = Array(size).fill(0).map((el, i) => i);
shuffle(arr);
return arr;
}
var arr = getRandomArray(1e6);
var min = arr[0];
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] < min) {
min = arr[i];
}
}
var min = arr[0];
var l = arr.length;
for (var i = 0; i < l; i += 1) {
if (arr[i] < min) {
min = arr[i];
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For | |
Math.min |
Test name | Executions per second |
---|---|
For | 3.1 Ops/sec |
Math.min | 6.1 Ops/sec |
Let's dive into explaining the provided benchmark.
Benchmark Definition JSON
The benchmark definition defines two JavaScript microbenchmarks: "For" and "Min1". The Script Preparation Code
section contains two functions:
shuffle(array)
: This function shuffles an array using a Fisher-Yates shuffle algorithm. It's used to generate a random array for the benchmarks.getRandomArray(size)
: This function creates an array of a specified size, fills it with numbers from 0 to the size minus one, and then shuffles it using the shuffle(array)
function.The Html Preparation Code
section is empty, indicating that this benchmark doesn't require any HTML preparation code.
Test Cases
There are two test cases:
var min = arr[0]; for (var i = 0; i < arr.length; i += 1) { if (arr[i] < min) { min = arr[i]; } }
* "Math.min": This benchmark tests the performance of using the built-in `Math.min()` function to find the minimum value in an array.
```javascript
var min = arr[0];
var l = arr.length;
for (var i = 0; i < l; i += 1) {
if (arr[i] < min) {
min = arr[i];
}
}
Options Compared
The benchmark compares two approaches:
Math.min()
function: The "Math.min" test case uses the built-in Math.min()
function to find the minimum value in an array.Pros and Cons
Here's a brief overview of the pros and cons of each approach:
Math.min()
function isn't available or desired.Math.min()
function (Math.min):Math.min()
function isn't available or desired.Library
There is no library used in this benchmark. However, note that some JavaScript implementations may provide additional functions or optimizations that could affect performance.
Special JS Features/Syntax
None of the provided benchmarks use any special JavaScript features or syntax.
Other Alternatives
If you're interested in exploring alternative approaches for finding the minimum value in an array, here are a few examples:
Array.prototype.reduce()
method: This method can be used to find the minimum value in an array by providing a custom reduction function.
arr.reduce((min, current) => min < current ? min : current)
* Using `Array.prototype.sort()` method with the `sort` option set to `ascending`: This method sorts the array and returns it. The first element is guaranteed to be the minimum value in an ascending sorted order.
```javascript
arr.sort((a, b) => a - b)[0]
Keep in mind that these alternatives may have different performance characteristics compared to the manual loop and Math.min()
function approaches.