bad random grab bag

Benchmark created on


Preparation HTML

<script>
  var grabBag = [];
  for(var i = 0; i < 1000; i++){
  grabBag.push(i);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
bad-random-grab-bag
var a = [];
for (var i = 0; i < 750; i++) {
  var r = Math.floor(Math.random()*1001) + 1;
  if(!(r in a))
    a.push(r);
  else
    i--;
}
ready
maybe-better-grab-bag
// randomize order of elements with a sort function that randomly returns -1/0/1
grabBag.sort(function(xx,yy){ return Math.floor(Math.random() * 3) - 1; })

function getNextRandom(){
    return grabBag.shift();
};

var originalLength = grabBag.length;
for(var i = 0; i < originalLength .length; i++){
    console.log(getNextRandom());
}
ready

Revisions

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