fyshuffle (v7)

Revision 7 of this benchmark created on


Preparation HTML

<script>
function numberArray(a,b){
 b=[];while(a--)b[a]=a+1;return b
}
var array100=numberArray(100),
    array1000=numberArray(1000);

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;
  var rnd=Math.random;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = (rnd() * currentIndex)|0;
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

function shuffleArray(array) {
    var j=0, temp, rnd=Math.random;
    for (var i = array.length - 1; i > 0; i--) {
        j = (rnd() * (i + 1))|0;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}

Array.prototype.shuffle = function() {
  var i = this.length, j, temp;
  var rnd=Math.random;
  if ( i == 0 ) return this;
  while ( --i ) {
     j = ( rnd() * ( i + 1 ) )|0;
     temp = this[i];
     this[i] = this[j];
     this[j] = temp;
  }
  return this;
}

function fy(a,b,c,d){var rnd=Math.random;
 c=a.length;while(c)b=rnd()*c--|0,d=a[c],a[c]=a[b],a[b]=d
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
fy
fy(array100);
fy(array1000)
ready
shuffleArray
shuffleArray(array100);
shuffleArray(array1000)
ready
shuffle prototype
array100.shuffle();
array1000.shuffle()
ready
shuffle
shuffle(array100);
shuffle(array1000)
ready

Revisions

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