Script Preparation code:
AخA
 
var arr1 = [];
for(let i = 0; i < 10000; i++) {
  arr1.push(Math.floor(Math.random() * 5000));
}
var arr2 = arr1.slice(0);
arr2.sort((a, b) => a - b);
Tests:
  • Unsorted

     
    var toSort = arr1.slice(0);
    toSort.sort((a, b) => a - b);
  • Sorted with check

     
    var toSort = arr2.slice(0);
    let lastEntry;
    let isSorted = true;
    for(const entry of toSort) {
      if(lastEntry != null && entry < lastEntry) {
        isSorted = false;
        break;
      }
      lastEntry = entry;
    }
    if(!isSorted) {
      toSort.sort((a, b) => a - b);
    }
  • Sorted without check

     
    var toSort = arr2.slice(0);
    toSort.sort((a, b) => a - b);
  • Unsorted with check

     
    var toSort = arr1.slice(0);
    let lastEntry;
    let isSorted = true;
    for(const entry of toSort) {
      if(lastEntry != null && entry < lastEntry) {
        isSorted = false;
        break;
      }
      lastEntry = entry;
    }
    if(!isSorted) {
      toSort.sort((a, b) => a - b);
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Unsorted
    Sorted with check
    Sorted without check
    Unsorted with check

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 13 days ago)
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Test name Executions per second
Unsorted 216.2 Ops/sec
Sorted with check 23828.3 Ops/sec
Sorted without check 2419.5 Ops/sec
Unsorted with check 175.0 Ops/sec