Array shuffle comparator

Benchmark created by marlanga on


Setup

var arr = [];
  for (var i = 0; i < 100000; i++) {
    arr[i] = i;
  }
  Array.prototype.mezclar = function() {
    var n = this.length;
    while (n--) {
      var i = Math.floor(n * Math.random());
      var tmp = this[i];
      this[i] = this[n];
      this[n] = tmp;
  
    }
    return this;
  }
  
  var mezclar2 = function(arr) {
      for (var i, tmp, n = arr.length; n; i = Math.floor(Math.random() * n), tmp = arr[--n], arr[n] = arr[i], arr[i] = tmp);
      return arr;
      }
      
      
      
  Array.prototype.mezclar3 = function() {
    for (var i, tmp, n = this.length; n--; i = Math.floor(Math.random() * n), tmp = this[i], this[i] = this[n], this[n] = tmp);
    return this;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
marlanga
arr.mezclar();
ready
emprear
arr = mezclar2(arr);
ready
m+e
arr.mezclar3();
ready

Revisions

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