Shuffle array (v2)

Revision 2 of this benchmark created by polotek on


Description

A few simple methods of randomly shuffling an array.

Preparation HTML

<script>
  var array = [], len = 10000;
  while(len--) {
    array[len] = len;
  }
  function sortShuffle(arr) {
    return arr.sort(function() { return Math.random() - 0.5; });
  }
  
  function indexShuffle(arr) {
    var len = cur = arr.length,
        newArr = new Array(len);
    while(cur--) {
      newArr[ Math.random() * len ] = arr[cur];
    }
    return newArr;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
sort shuffle
sortShuffle(array);
ready
index shuffle
indexShuffle(array);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.