numbers.js sum

Benchmark created on


Setup

var numbers = {};
    numbers.sum = function (arr) {
      if (Object.prototype.toString.call(arr) === '[object Array]') {
        var total = 0;
        for (var i = 0 ; i < arr.length ; i++) {
          if (typeof(arr[i]) === 'number') {
            total = total + arr[i];
          } else {
            throw new Error('All elements in array must be numbers');
          }
        }
        return total;
      } else {
        throw new Error('Input must be of type Array');
      }
    };
    var confident = {};
    confident.sum = function (arr) {
      var total = 0;
      for (var i = 0, l = arr.length; i < l; i++) {
        total += arr[i];
      }
      return total;
    };
    var a = [];
    for (var i = 0; i < 10000; i++) {
        a.push(Math.round(Math.random() * 100));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
numbers
numbers.sum(a);
ready
confident
confident.sum(a);
ready

Revisions

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