Loop Unrolling (v6)

Revision 6 of this benchmark created on


Setup

const count = 1e6;
const data = Array.from({length: count}).map(() => Math.random());

const simpleSum = (data) => {
  let sum = 0;
  for(let i=0; i < data.length; i += 1) {
    sum += data[i];
  }
  return sum;
}

const sumByTwo = (data) => {
  let sum = 0
  for(let i=0; i < data.length; i += 2) {
    sum += data[i] + data[i + 1];
  }
  return sum;
}

const sumByFour = (data) => {
  let sum = 0
  for(let i=0; i < data.length; i += 4) {
    sum += data[i]
      + data[i + 1]
      + data[i + 2]
      + data[i + 3];
  }
  return sum;
}

const sumByTen = (data) => {
  let sum = 0;
  for(let i=0; i < data.length; i += 10) {
    sum += data[i] + data[i + 1]
      + data[i + 2] + data[i + 3]
      + data[i + 4] + data[i + 5]
      + data[i + 5] + data[i + 7]
      + data[i + 8] + data[i + 9];
  }
  return sum;
};

Test runner

Ready to run.

Testing in
TestOps/sec
Regular
simpleSum(data);
ready
By Two
sumByTwo(data);
ready
By Four
sumByFour(data);
ready
By Ten
sumByTen(data);
ready

Revisions

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