lodash resuce vs for

JavaScript performance comparison

Test case created and last updated

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script>
Benchmark.prototype.setup = function() {
    var _ = window.lodash;
    var sections = [{
      length: 123
    }, {
      length: 12,
      is_final: true
    }, {
      length: 0
    }, {
      length: 132,
      is_final: true
    }, {
      length: 40,
      is_final: true
    }, {
      length: 5
    }, {
      length: 440,
      is_final: true
    }, {
      length: 15
    }, {
      length: 1,
      is_final: true
    }, {
      length: 6,
      is_final: true
    }, {
      length: 8
    }, {
      length: 4
    }, {
      length: 6,
      is_final: true
    }, {
      length: 1
    }, {
      length: 4
    }, {
      length: 6,
      is_final: true
    }, {
      length: 4
    }, {
      length: 6
    }, {
      length: 6
    }, {
      length: 1
    }, {
      length: 18,
      is_final: true
    }, {
      length: 1
    }];
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
_
_.reduce(sections, function(sum, section) {
  return section.is_final ? sum : sum + section.length;
}, 0);
pending…
for
var sum = 0;
for (var i = 0, l = sections.length; i < l; i++) {
  if (!sections[i].is_final) sum += sections[i].length
}
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