sku组合算法性能测试

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var saleProp = [
      ['A', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
      ['D', 'E', 'F', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
      ['H', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
      ['J', 'K', 'L', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
  ];
  function concatAll(items){
      var base = items[0]
  
      return mulit(base, items.slice(1))
  
      function mulit(base, leftArr){
          var multiplier = leftArr[0];
  
          var newBase = [];
          for(var i = 0, len = base.length; i < len; i++){
              var b = base[i];
              for(var j = 0, len2 = multiplier.length; j < len2; j++){
                  var m = multiplier[j];
                  newBase.push(b+''+m);
              }
          }
  
          var _left = leftArr.slice(1);
          if(_left.length){
              return mulit(newBase, _left);
          }else{
              return newBase;
          }
      }
  }
  
  function concatAll2(arr) {
      var funcBody = 'var ret = [];',
          IDX = ['i', 'j', 'k', 'l'];
  
      for (var i = 0, len = arr.length; i < len; i++) {
          var idx = IDX[i];
  
          funcBody += 'for (var ' + idx + ' = 0;' + idx + '< arr[' + i + '].length;' + idx + '++){';
          if (i == len - 1) {
              var mash = [];
              for (var j = 0; j < arr.length; j++) {
                  var idx2 = IDX[j]
                  mash.push('arr[' + j + '][' + idx2 + ']');
              }
              funcBody += 'ret.push(' + mash.join('+') + ')';
          }
      }
  
      for (var i = 0, len = arr.length; i < len; i++) {
          funcBody += '}';
      }
  
      funcBody += 'return ret;'
  
      return (new Function('arr', funcBody)(arr));
  }
  
  
  

};
</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
递归
concatAll(saleProp);
pending…
动态创建函数
concatAll2(saleProp);
pending…

Compare results of other browsers

Revisions

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

0 Comments