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 |
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);
var toSort = arr1.slice(0);
toSort.sort((a, b) => a - b);
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);
}
var toSort = arr2.slice(0);
toSort.sort((a, b) => a - b);
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);
}