euler1

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  /*Generate a list of all Combinations*/
  function getCombinations(list){
      var combinations = []; //All combinations
      var combination = [];  //Single combination
      var quantity = (1 << list.length);
      for (var i = 0; i < quantity ; i++){
          combination = [];
          for (var j=0;j<list.length;j++) {
              if ((i & (1 << j))){ 
                  combination.push(list[j]);
              }
          }
          if (combination.length !== 0) {
              combinations.push(combination);
          }
      }
      return combinations;
  }
  
  /*Generate a product from a list of numbers*/
  function listProduct(list){
      var product=1;
      for (var i=0;i<list.length;i++){
          product *=list[i];
      }
      return product;
  }
  
  /*Return the arithmetic sum*/
  function arithmeticSum (a, n){
      return (1/2)*n*(a+a*n);
  }
  
  /*Generate the arithmetic sum of a series based on a multiple*/
  function arithmeticSumFromSeriesMultiple (series, multiple){
      var a = multiple;
      var n = Math.floor(series/multiple);
      return arithmeticSum(a, n);
  }
  
      
  function sumMultiples(range, multiples){
      var sum= 0;
      var subsetSums = [];
      
      var multiplesCombination=getCombinations(multiples);
      for (var i=0;i<multiplesCombination.length;i++){
          
          //Generate product from combinations of multiples
          //and
          //Find individual sums of all combinations.
          subsetSums.push(
              arithmeticSumFromSeriesMultiple(
                  range, 
                  listProduct(multiplesCombination[i])
              )
          );
      }
  
      for (var i=1; i< subsetSums.length + 1;i++){
          //Check if i is an even base 2.
          if ((i & (i - 1)) == 0){
              console.log(">" + subsetSums[i-1])
              sum += subsetSums[i-1];
          } else {
              console.log("<" + subsetSums[i-1])
              sum -= subsetSums[i-1];
          }
      }
      return sum;
  }
  
  function sumMultiples2(n, multiples){
      var sum= 0;
      var isMultiple = function(n, multiple){
          return ((n % multiple) === 0);
      };
      
      var commonMultiples = function (n,multiples){
          var isCommon = false;
          for (var i=0;i<multiples.length;i++){
              isCommon = (isCommon || isMultiple(n, multiples[i]));
          }
          return isCommon;
      };
      for (var i=1;i<n;i++) {
          if (commonMultiples (i,multiples)) {
              sum += i;
          }
      }
      return sum;
  }

};
</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
nomath small
sumMultiples2(1000, [3,5])
pending…
math small
sumMultiples(999, [3,5])
pending…
nomath big
//This function crashes the VM when larger than this
sumMultiples2(1000000000, [3,5,11,101])
pending…
math big
sumMultiples(1000000000-1, [3,5,11,101])
pending…
math huge
//Doesn't work with nomath.
sumMultiples(100000000000000-1, [3,5,11,101])
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