const array = [423, 654, 123, 765, 436, 142, 976, 413];
const test = array.sort((a, b) => a-b).reverse();
const array = [423, 654, 123, 765, 436, 142, 976, 413];
const test = array.sort((a,b) => b-a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
test1 | |
test2 |
Test name | Executions per second |
---|---|
test1 | 3416042.2 Ops/sec |
test2 | 3516341.8 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of two approaches to find the maximum value in an array: using the built-in Math.max()
function versus sorting and reversing the entire array.
Options compared:
Math.max()
: This is a built-in JavaScript function that returns the largest of zero or more numbers.sort() + reverse()
): This approach involves first sorting the array in ascending order using the sort()
method, which rearranges the elements in place, and then reversing the sorted array using the reverse()
method.Pros and Cons:
Math.max()
:sort() + reverse()
):Math.max()
due to the overhead of sortingOther considerations:
Math.max()
might not work correctly.Library usage:
None of the test cases use any external libraries. However, if you were to add additional functionality or optimizations, you might consider using libraries like Lodash or Underscore.js for utility functions.
Special JS features:
None mentioned in this benchmark.
Benchmark preparation code:
The Script Preparation Code is empty, indicating that no specific setup or initialization code is required for the test cases. The Html Preparation Code is also empty.
Alternatives:
Other approaches to find the maximum value in an array include:
reduce()
: This method applies a user-supplied function to each element of the array and reduces it to a single output value.Keep in mind that these alternative approaches may have different performance characteristics compared to Math.max()
and the sorting and reversing approach.