Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs .map().sort().reverse()
(version: 0)
Comparing performance of:
test1 vs test2
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
test1
const array = [423, 654, 123, 765, 436, 142, 976, 413]; const test = array.sort((a, b) => a-b).reverse();
test2
const array = [423, 654, 123, 765, 436, 142, 976, 413]; const test = array.sort((a,b) => b-a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
test1
test2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
test1
3416042.2 Ops/sec
test2
3516341.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**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:** 1. **`Math.max()`**: This is a built-in JavaScript function that returns the largest of zero or more numbers. 2. **Sorting and Reversing (`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:** 1. **`Math.max()`**: * Pros: + Fast and efficient + Fewer operations required (only a single function call) * Cons: + May not work as expected for arrays with non-numeric values or complex data structures 2. **Sorting and Reversing (`sort() + reverse()`)**: * Pros: + Works for all arrays, regardless of data type or structure * Cons: + Requires two additional operations (sorting and reversing) + May be slower than `Math.max()` due to the overhead of sorting **Other considerations:** 1. **Array size**: The benchmark assumes an array with a moderate number of elements (8 in this case). For larger arrays, the sorting and reversing approach may become even more inefficient. 2. **Data type**: The benchmark uses only numeric values. If the array contained non-numeric values, `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: 1. **Using `reduce()`**: This method applies a user-supplied function to each element of the array and reduces it to a single output value. 2. **Using a custom implementation with loops**: You could write your own loop-based implementation to iterate through the array and keep track of the maximum value found so far. Keep in mind that these alternative approaches may have different performance characteristics compared to `Math.max()` and the sorting and reversing approach.
Related benchmarks:
Get max from an array of numbers (Math.max vs. iteration)
Get max from an array of numbers (Math.max vs. iteration) V2
Get max from an array of numbers (Math.max vs. iteration) V3
sorting an array and then reverse vs sorting with reversive comparison
Comments
Confirm delete:
Do you really want to delete benchmark?