Summing array elements: underscore reduce vs javascript reduce vs for (v20)

Revision 20 of this benchmark created by Thomas Hondema on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js"></script>
<script>
lodash = _.noConflict();
</script>
<script src="http://underscorejs.org/underscore.js"></script>

Setup

var ar = _.range(10000)
    var c = ar.length
    var sum = 0

Test runner

Ready to run.

Testing in
TestOps/sec
underscore reduce
_.reduce(ar, function(sum, el) {
  return sum + el
}, 0)
ready
for loop
var c = ar.length
var sum = 0
for (var i = 0; i < c; i++) {
  sum += ar[i]
}
ready
javascript reduce
ar.reduce(function(sum, el) {
  return sum + el
}, 0)
ready
while loop
var c = ar.length
var sum = 0
while (c-- >= 0) {
  sum += ar[c]
}
ready
lodash sum
lodash.sum(ar)
ready
for loop 2
var c = ar.length
var sum = 0
for (; c >= 0; c--) {
  sum += ar[c]
}
ready

Revisions

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