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

Revision 6 of this benchmark created on


Preparation HTML

<script src="https://raw.github.com/lodash/lodash/2.4.1/dist/lodash.min.js">
</script>

Setup

var A = _.range(100000);
    var I = A.length;
    var i;
    var v;

Test runner

Ready to run.

Testing in
TestOps/sec
underscore reduce
_.reduce(A, function(v, e) {
  return v + e
}, 0)
ready
for loop
i = 0;
v = 0;
for (i; i < I; i++) {
  v += A[i]
}
ready
javascript reduce
A.reduce(function(v, e) {
  return v + e
}, 0)
ready
while (1)
// Joerg_Schaper@gmx.de
i = I;
v = 0;
while (i > 7) v += A[--i] + A[--i] + A[--i] + A[--i] + A[--i] + A[--i] + A[--i] + A[--i];
while (i > 0) v += A[--i];
ready
while (2)
// Joerg_Schaper@gmx.de
i = I;
v = 0;
while (i > 7) v += A[i - 1] + A[i - 2] + A[i - 3] + A[i - 4] + A[i - 5] + A[i - 6] + A[i - 7] + A[i -= 8];
while (i > 0) v += A[--i];
ready
while (3)
// Joerg_Schaper@gmx.de
i = I;
v = 0;
while (i > 7) v += A[i - 1] + A[i - 2] + A[i - 3] + A[i - 4] + A[i - 5] + A[i - 6] + A[i - 7] + A[i -= 8];
switch (i) {
  case (7):
    v += A[6];
  case (6):
    v += A[5];
  case (5):
    v += A[4];
  case (4):
    v += A[3];
  case (3):
    v += A[2];
  case (2):
    v += A[1];
  case (1):
    v += A[0];
};
ready
while (4)
// Joerg_Schaper@gmx.de
i = I;
v = 0;
while (i > 7) v += A[i - 1] + A[i - 2] + A[i - 3] + A[i - 4] + A[i - 5] + A[i - 6] + A[i - 7] + A[i -= 8];
switch (i) {
  case (0):
    break;
  case (1):
    v += A[0];
    break;
  case (2):
    v += A[0] + A[1];
    break;
  case (3):
    v += A[0] + A[1] + A[2];
    break;
  case (4):
    v += A[0] + A[1] + A[2] + A[3];
    break;
  case (5):
    v += A[0] + A[1] + A[2] + A[3] + A[4];
    break;
  case (6):
    v += A[0] + A[1] + A[2] + A[3] + A[4] + A[5];
    break;
  case (7):
    v += A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6];
    break;
};
ready
while (5)
// Joerg_Schaper@gmx.de
i = I;
v = 0;
while (i > 7) v += A[i - 1] + A[i - 2] + A[i - 3] + A[i - 4] + A[i - 5] + A[i - 6] + A[i - 7] + A[i -= 8];
switch (i) {
  case (0):
    break;
  case (1):
    v += A[0];
    break;
  case (2):
    v += A[1] + A[0];
    break;
  case (3):
    v += A[2] + A[1] + A[0];
    break;
  case (4):
    v += A[3] + A[2] + A[1] + A[0];
    break;
  case (5):
    v += A[4] + A[3] + A[2] + A[1] + A[0];
    break;
  case (6):
    v += A[5] + A[4] + A[3] + A[2] + A[1] + A[0];
    break;
  case (7):
    v += A[6] + A[5] + A[4] + A[3] + A[2] + A[1] + A[0];
    break;
};
ready

Revisions

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