for vs reduce

Benchmark created on


Setup

const arr = new Array(100).fill(0).map((e, i) => i);
const date = Date.now()

Test runner

Ready to run.

Testing in
TestOps/sec
Loop for
  let j = 0;
  let i = 0;
  let res = 0;
  const arrLen = arr.length;

  for (j = 0; j < 10000000; j++) {
    for (i = 0; i < arrLen; i++) {
			if (date - arr[i] > 0) {
        res += arr[i];
			}
    }
  }
ready
reduce
  let j = 0;
  let res = 0;

  for (j = 0; j < 10000000; j++) {
    res += arr.reduce((p, c) => {
      if (date - c > 0) {
        return p + c;
      }
    }, 0);
  }
ready
for of
  let j = 0;
  let i = 0;
  let res = 0;
  const arrLen = arr.length;

  for (j = 0; j < 10000000; j++) {
  	for (const el of arr) {
		if (date - el > 0) {
   			res += el;
		}
	}
  }
ready

Revisions

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