zerkms-stackoverflow-20647297

Benchmark created by trivial-jquery-js-selector on


Test runner

Ready to run.

Testing in
TestOps/sec
z
function ArrayAdditionI(arr) { 

  for (var i = 0; i < arr.length; i++) {
    arr[i] = parseInt(arr[i], 10);
  }
   
    arr.sort(function(a, b) { return a - b; });

  var max = arr[arr.length - 1];

  //everything above makes sense
  for (var i = 0, l = 1 << (arr.length - 1); i < l; i++) {
    var s = 0;
    for (var j = arr.length - 2; j >= 0; j--) {
      if (i & (1 << j)) {
        s += arr[j];
          
          if (s > max) {
              break;
          }
      }
    }

    if (s == max) {
      return true;
    }
  }

  return false;       
}

var r = ArrayAdditionI([4, 6, 23, 10, 1, 3])  
 
ready
o
function ArrayAdditionI(arr) { 

  for (var i = 0; i < arr.length; i++) {
    arr[i] = parseInt(arr[i], 10);
  }

  var max = arr[0];
  for (var i = 1; i < arr.length; i++) {
    max = Math.max(max, arr[i]);
  }
  //everything above makes sense
  for (var i = 0, l = 1 << arr.length; i < l; i++) {
    var s = 0, cnt = 0;
    for (var j = 0; j < arr.length; j++) {
      if (i & (1 << j)) {
        s += arr[j];
        cnt++;
      }
    }

    if (cnt > 1 && s == max) {
      return true;
    }
  }

  return false;       
}

ArrayAdditionI([4, 6, 23, 10, 1, 3]) 
ready

Revisions

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

  • Revision 1: published by trivial-jquery-js-selector on