numbers.js sum

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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));
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
numbers
numbers.sum(a);
pending…
confident
confident.sum(a);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment