Sorting Time

function fastSort(scores, max) {
  "use strict";
  const sparseArr = [];
  
  for (let i = 0, l = scores.length; i < l; i++) {
    let diff = max - scores[i];
    sparseArr[diff] = scores[i];
  }
  
  return sparseArr.filter(ele => ele);
}

function slowSort(scores) {
  "use strict";
  
  return scores.sort((a, b) => b - a);
}

const max = 1000000;
const dataSet = [];

// Fill data set with 10,000 entries.
for (let i = 0; i < 10000; i++) {
  dataSet.push(Math.floor(Math.random() * max));
}

const slowStart = performance.now();
const slowResult = slowSort(dataSet);
const slowStop = performance.now();

const fastStart = performance.now();
const fastResult = fastSort(dataSet, max);
const fastStop = performance.now();

console.log("Slow? > ", slowResult.length, (slowStop - slowStart) + "ms");
console.log("Fast? > ", fastResult.length, (fastStop - fastStart) + "ms");