title: What is the fastest way to sort a large(ish) array of numbers in JavaScript
body: <p>In my application, I need to sort large arrays (between 100,000 and 1,000,000) of random numbers.</p>
  
  <p>I've been using the built in <code>array.sort(comparisonFunction)</code> where comparisonFunction looks like this:</p>
  
  <pre><code>function comparisonFunction(a,b) {
      return a-b;
  }
  </code></pre>
  
  <p>This works just fine, but I've read (e.g., <a href="https://stackoverflow.com/questions/38732480/native-javascript-sort-performing-slower-than-implemented-mergesort-and-quicksor">Native JavaScript sort performing slower than implemented mergesort and quicksort</a>) that there are faster options, especially if your requirements meet certain conditions:</p>
  
  <ol>
  <li><strong><em>I only need to sort numbers (e.g., not objects, or alphanumeric data)</em></strong></li>
  <li><strong><em>The data is random (no chance that it's already ordered)</em></strong></li>
  <li><strong><em>The sort doesn't need to be stable</em></strong></li>
  </ol>
  
  <p>So - what is the fastest (or close enough) sort algorithm available under those circumstances?</p>
  
  <p>And, is there a canonical (or at least a relatively ideal) JavaScript implementation?</p>
  
  <p>[UPDATE]</p>
  
  <p>So, a quick clarification - in the linked question, the OP required a stable sort. Since I don't - I'm wondering if that would change the answer (i.e., perhaps there's a faster sort option available <strong><em>if you know in advance that your data will not be pre-sorted, and you don't need a stable sort</em></strong>).</p>
  
  <p>Perhaps the answer is "no", but that's why I'm asking.</p>
  
  <p>[UPDATE #2]</p>
  
  <p>Here's an implementation of quicksort that, unless I've made a mistake - beats the native sort function handily:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function comparisonFunction(a, b) {
    return a - b;
  }
  
  function quickSort(arr, leftPos, rightPos, arrLength) {
    let initialLeftPos = leftPos;
    let initialRightPos = rightPos;
    let direction = true;
    let pivot = rightPos;
    while ((leftPos - rightPos) &lt; 0) {
      if (direction) {
        if (arr[pivot] &lt; arr[leftPos]) {
          quickSort.swap(arr, pivot, leftPos);
          pivot = leftPos;
          rightPos--;
          direction = !direction;
        } else
          leftPos++;
      } else {
        if (arr[pivot] &lt;= arr[rightPos]) {
          rightPos--;
        } else {
          quickSort.swap(arr, pivot, rightPos);
          leftPos++;
          pivot = rightPos;
          direction = !direction;
        }
      }
    }
    if (pivot - 1 &gt; initialLeftPos) {
      quickSort(arr, initialLeftPos, pivot - 1, arrLength);
    }
    if (pivot + 1 &lt; initialRightPos) {
      quickSort(arr, pivot + 1, initialRightPos, arrLength);
    }
  }
  quickSort.swap = (arr, el1, el2) =&gt; {
    let swapedElem = arr[el1];
    arr[el1] = arr[el2];
    arr[el2] = swapedElem;
  }
  
  var
    i,
    arr1, arr2,
    length;
  
  length = 1000000;
  
  
  arr1 = [];
  arr2 = [];
  for (i = 0; i &lt; length; i++) {
    arr1.push(Math.random());
    arr2.push(Math.random());
  }
  
  console.time("nativeSort");
  arr1.sort(comparisonFunction);
  console.timeEnd("nativeSort");
  
  
  console.time("quickSort");
  quickSort(arr2, 0, length - 1, length);
  console.timeEnd("quickSort");</code></pre>
  </div>
  </div>
  </p>