Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sorting an array and then reverse vs sorting with reversive comparison
(version: 0)
Comparing performance of:
sorting an array and then reverse vs sorting with reversive comparison
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
sorting an array and then reverse
const arr = [ 83, 52, 100, 58, 20, 41, 53, 19, 27, 60, 91, 2, 29, 78, 45, 49, 56, 98, 35, 66, 63, 79, 3, 61, 75, 59, 71, 65, 34, 1, 81, 74, 30, 5, 97, 57, 62, 31, 92, 43, 16, 55, 32, 8, 46, 68, 40, 12, 87, 82, 80, 54, 96, 14, 94, 88, 72, 67, 24, 85, 76, 15, 18, 17, 64, 11, 51, 25, 38, 21, 48, 28, 39, 7, 89, 33, 26, 73, 90, 95, 36, 10, 47, 6, 69, 22, 4, 86, 44, 9, 93, 37, 84, 42, 99, 23, 77, 13, 70, 50]; const case1 = [...arr].sort( (a,b) => a - b).reverse();
sorting with reversive comparison
const arr = [ 83, 52, 100, 58, 20, 41, 53, 19, 27, 60, 91, 2, 29, 78, 45, 49, 56, 98, 35, 66, 63, 79, 3, 61, 75, 59, 71, 65, 34, 1, 81, 74, 30, 5, 97, 57, 62, 31, 92, 43, 16, 55, 32, 8, 46, 68, 40, 12, 87, 82, 80, 54, 96, 14, 94, 88, 72, 67, 24, 85, 76, 15, 18, 17, 64, 11, 51, 25, 38, 21, 48, 28, 39, 7, 89, 33, 26, 73, 90, 95, 36, 10, 47, 6, 69, 22, 4, 86, 44, 9, 93, 37, 84, 42, 99, 23, 77, 13, 70, 50]; const case2 = [...arr].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
sorting an array and then reverse
sorting with reversive comparison
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sorting an array and then reverse
290203.2 Ops/sec
sorting with reversive comparison
305000.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test cases. **Benchmark Definition** The benchmark is designed to compare two approaches for sorting an array and then reversing it. The goal is to determine which approach is faster. **Options Compared** There are two options being compared: 1. **Sorting an array and then reversing it**: This approach sorts the array in ascending order and then reverses the sorted array using the `reverse()` method. 2. **Sorting with reverse comparison**: This approach uses a custom comparison function that returns the negative of the difference between two elements, effectively sorting the array in descending order. **Pros and Cons** **Approach 1: Sorting an array and then reversing it** Pros: * Easy to understand and implement * Works well for small arrays Cons: * Has to sort the entire array first, which can be time-consuming * Reversing the sorted array can also be slow **Approach 2: Sorting with reverse comparison** Pros: * Can take advantage of the CPU's natural sorting algorithm, making it potentially faster * May be more efficient for large arrays Cons: * Requires a custom comparison function, which can add complexity * May not work well for certain types of data or edge cases **Library/Functionality Used** In both test cases, the `sort()` and `reverse()` methods are used from the JavaScript Array prototype. These methods are built-in to most modern browsers and provide efficient sorting algorithms. **Special JS Feature/Syntax** None mentioned in this specific benchmark. However, note that custom comparison functions like `(a,b) => b - a` are used for the reverse comparison approach. **Other Alternatives** There are other approaches to sorting arrays, such as: * Using `Array.prototype.sort()` with a different comparison function (e.g., `(a,b) => a - b` for ascending order) * Using external libraries like Lodash or Ramda for sorting * Implementing custom sorting algorithms (e.g., quicksort, mergesort) In general, the choice of sorting algorithm depends on the specific use case and requirements. For most cases, built-in methods like `Array.prototype.sort()` are sufficient and efficient.
Related benchmarks:
pre-sorted sorting
Math.max() vs .map().sort().reverse()
JavaScript reverse vs toReversed
JavaScript reverse with copy vs toReversed
Comments
Confirm delete:
Do you really want to delete benchmark?