underscore sum by

JavaScript performance comparison

Test case created by axelarge

Preparation code

<script src="http://underscorejs.org/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var thingies = [],
        i = 1000;
    while (--i) {
      thingies.push(i);
    }
    _.mixin({
      sumBy: function(array, fn) {
        var sum = 0;
        _.each(array, function(element) {
          sum += fn(element);
        });
        return sum;
      },
   
      sumBy2: function(array, fn) {
        return _.reduce(array, function(acc, elem) {
          return acc + fn(elem);
        }, 0);
      }
    });
   
    function fn(x) {
      return x * 2;
    }
};
</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
each
_.sumBy(thingies, fn)
pending…
reduce
_.sumBy2(thingies, fn)
pending…

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

Compare results of other browsers

0 comments

Add a comment