Array Shuffle

Benchmark created by John-David Dalton on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
<script>
  var small = [1, 2, 3];
  
  var medium = Array(101).join('x').split('');
  
  var large = Array(1001).join('x').split('');
  
  Array.prototype.shuffle1 = function() {
   return this.sortBy(Math.random);
  };
  
  Array.prototype.shuffle2 = function() {
   var i, tmp, object = this.slice(0),
       length = object.length;
   while (length--) {
    i = (Math.random() * length | 0) + 1;
    tmp = object[i]; /* tmp var is faster than array trick */
    object[i] = object[length]
    object[length] = tmp;
   }
   return object;
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Shuffle (Fuchs)
small.shuffle1();
medium.shuffle1();
large.shuffle1();
ready
Suffle (Fisher-Yates)
small.shuffle2();
medium.shuffle2();
large.shuffle2();
ready

Revisions

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

  • Revision 1: published by John-David Dalton on