var v1 = 12345678;
var v2 = 23456789;
var arr = [v2, v1]
var a = arr.sort();
var min = a[0];
var max = a[1];
var min = Math.min(v2, v1);
var max = Math.max(v2, v1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.sort | |
Math min and max |
Test name | Executions per second |
---|---|
Array.sort | 1544778.2 Ops/sec |
Math min and max | 858839.8 Ops/sec |
Let's break down the benchmark definition and test cases.
Benchmark Definition
The website is testing two different approaches to find the minimum and maximum values in an array: Array.sort()
followed by accessing the first and last elements, and using the built-in Math.min()
and Math.max()
functions.
Script Preparation Code
The script preparation code creates an array arr
with two elements, v1
and v2
, which are assigned values. The intention is to create a small, predictable dataset for testing.
Html Preparation Code
There is no html preparation code provided, which suggests that the benchmark only concerns the JavaScript execution and not other aspects like rendering or layout.
Individual Test Cases
The test cases are designed to compare the performance of two different approaches:
min
) and last element (max
). The sort()
method is a stable sort, meaning that equal elements will keep their original order.Math.min()
and Math.max()
functions to find the minimum and maximum values in the array.Pros and Cons of Each Approach
Other Considerations
sort()
method is stable, meaning that equal elements will keep their original order. This can be beneficial in certain scenarios where preserving the original order of equal elements is important.Library and Special JS Features
There are no libraries used in this benchmark, but it's worth noting that the Math.min()
and Math.max()
functions are part of the JavaScript standard library.
Other Alternatives
If you need to find min/max values in an array without using Array.sort()
, other approaches could include:
Keep in mind that these alternatives may have their own trade-offs and considerations depending on the specific use case.