Combinations

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Coffee
var combi = ({
  combinationsSync: function(iterable, r) {
    var broken, doYield, i, indices, j, n, pool, result, _i, _j, _k, _l, _len, _ref, _ref1, _results, _results1;
    pool = iterable;
    n = pool.length;
    if (!(r <= n)) {
      return [];
    }
    indices = (function() {
      _results = [];
      for (var _i = 0; 0 <= r ? _i < r : _i > r; 0 <= r ? _i++ : _i--){ _results.push(_i); }
      return _results;
    }).apply(this);
    result = [];
    doYield = function() {
      var i;
      return result.push((function() {
        var _j, _len, _results1;
        _results1 = [];
        for (_j = 0, _len = indices.length; _j < _len; _j++) {
          i = indices[_j];
          _results1.push(pool[i]);
        }
        return _results1;
      })());
    };
    doYield();
    while (true) {
      broken = false;
      _ref = (function() {
        _results1 = [];
        for (var _k = 0; 0 <= r ? _k < r : _k > r; 0 <= r ? _k++ : _k--){ _results1.push(_k); }
        return _results1;
      }).apply(this).reverse();
      for (_j = 0, _len = _ref.length; _j < _len; _j++) {
        i = _ref[_j];
        if (indices[i] !== i + n - r) {
          broken = true;
          break;
        }
      }
      if (!broken) {
        return result;
      }
      indices[i]++;
      for (j = _l = _ref1 = i + 1; _ref1 <= r ? _l < r : _l > r; j = _ref1 <= r ? ++_l : --_l) {
        indices[j] = indices[j - 1] + 1;
      }
      doYield();
    }
  }
});

combi.combinationsSync("0,1,2,3,4",1)
combi.combinationsSync("0,1,2,3,4",2)
combi.combinationsSync("0,1,2,3,4",3)
combi.combinationsSync("0,1,2,3,4",4)
 
pending…
Bittest
function bitprint(u) {
  var s="";
  for (var n=0; u; ++n, u>>=1)
    if (u&1) s+=n+" ";
  return s;
}
function bitcount(u) {
  for (var n=0; u; ++n, u=u&(u-1));
  return n;
}
function comb(c,n) {
  var s=[];
  for (var u=0; u<1<<n; u++)
    if (bitcount(u)==c)
      s.push(bitprint(u))
  return s.sort();
}
comb(1,5);
comb(2,5);
comb(3,5);
comb(4,5);
 
pending…
Three
function combinations(arr, k){
    var i,
    subI,
    ret = [],
    sub,
    next;
    for(i = 0; i < arr.length; i++){
        if(k === 1){
            ret.push( [ arr[i] ] );
        }else{
            sub = combinations(arr.slice(i+1, arr.length), k-1);
            for(subI = 0; subI < sub.length; subI++ ){
                next = sub[subI];
                next.unshift(arr[i]);
                ret.push( next );
            }
        }
    }
    return ret;
}
combinations([0,1,2,3,4], 1);
combinations([0,1,2,3,4], 2);
combinations([0,1,2,3,4], 3);
combinations([0,1,2,3,4], 4);
pending…
Original
var records = [];
function runTags(tags){
        function _setCriteria(criteria, oldChain){
                for(var a = 0; a < criteria.length; a++){
                        var criterion = criteria[a];

                        var childChain = oldChain.slice(0, oldChain.length);
                        childChain.push(criterion);

                        var child = childChain.join(',');
                        records.push(child);
                        var sliced = criteria.slice(a+1);
                        sliced.length && _setCriteria(sliced, childChain);
                }
        }
        var criteria = tags.split(',');
        _setCriteria(criteria, []);
}

runTags("0,1,2,3,4");
 
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

Add a comment