Lazy Cartesian Product Comparison

JavaScript performance comparison

Test case created by Gavin Kistner

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  $sets = [];
  for (var i=0;i<6;++i){
    var a = $sets[i] = [];
    for (var j=0;j<6;++j) a[j]=j+""+Math.random();
  }
  function callback(){}
    
  function LazyProduct(sets){
    for (var dm=[],f=1,l,i=sets.length;i--;f*=l){ dm[i]=[f,l=sets[i].length] }
    this.length = f;
    this.item = function(n){
      for (var c=[],i=sets.length;i--;)c[i]=sets[i][(n/dm[i][0]<<0)%dm[i][1]];
      return c;
    };
  };
  
  function lazyProduct(sets,f,context){
    if (!context) context=this;
    var p=[],max=sets.length-1,lens=[];
    for (var i=sets.length;i--;) lens[i]=sets[i].length;
    function dive(d){
      var a=sets[d], len=lens[d];
      if (d==max) for (var i=0;i<len;++i) p[d]=a[i], f.apply(context,p);
      else        for (var i=0;i<len;++i) p[d]=a[i], dive(d+1);
      p.pop();
    }
    dive(0);
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Direct Index
var combos = new LazyProduct($sets);
for (var i=combos.length;i--;){
  callback.apply(this,combos.item(i));
}
pending…
Fast Recursion
lazyProduct($sets,callback);
pending…

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

Compare results of other browsers

0 Comments