0

Which loop should be use for better optimisation ?

I have tried something to test

const arr = new Array(200).fill().map((e, i) => i);
let testArr = [];

function forLoopTest() {
  const t0 = performance.now();

  for (let i = arr.length - 1; i >= 0; i--) {
    testArr.push(arr[i]);
  }

  const t1 = performance.now();
  return (t1 - t0);
}

function forEachTest() {
  const t0 = performance.now();

  arr.forEach(i => testArr.push(i));

  const t1 = performance.now();
  return (t1 - t0);

}


let runTest = 10;
while (runTest > 0) {
  testArr = [];
  console.log(`${forLoopTest()} -- ${forEachTest()}`);
  runTest--;
}

My outputs are

"0 -- 0",

"0 -- 0",

"0 -- 0",

"0 -- 0.10000000111176632",

"0 -- 0",

"0 -- 0",

"0 -- 0",

"0 -- 0.10000000111176632",

"0 -- 0",

"0 -- 0"

My questions are that is quite different from other stackoverflow question

  1. Why execution time is different for same methods ?
  2. Can we assume that For loop is better as output suggested ?
  3. Is this right methods to test time execution ?
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
  • as far I know for-loop have better perf – Nazır Dogan Jan 09 '19 at 11:48
  • Check this https://stackoverflow.com/questions/43031988/javascript-efficiency-for-vs-foreach – Nazır Dogan Jan 09 '19 at 11:49
  • 3
    Please search for benchmarks at https://jsperf.com/ , or create your own, they're more accurate than your computer doing this and that during your tests. – Teemu Jan 09 '19 at 11:49
  • They are very very similar, I guess it really just depends on what it is you're doing in the loop. I tend to use for loops over forEach in JS. If I'm modifying data I'll use map, if I'm reducing an array I'll use filter. –  Jan 09 '19 at 11:49
  • 2
    That’s a strange test. Not really comparable, because your `for` loop isn’t even looping over an array. Anyway, nowadays, the performance differences of `for`, `forEach`, and `for of` are so small you don’t need to worry about them most of the time. – Ry- Jan 09 '19 at 11:54
  • "better optimisation" here is almost certainly [premature optimisation](https://softwareengineering.stackexchange.com/questions/80084/is-premature-optimization-really-the-root-of-all-evil) and therefore evil – Liam Jan 09 '19 at 11:54
  • how ? it is looping arr @Ry- – Ashutosh Jha Jan 09 '19 at 11:55
  • 1
    You should at least do the same things in both loops `testArr.push(arr[i]);` instead of `testArr.push(i);` – Yury Tarabanko Jan 09 '19 at 11:56
  • 1
    @AshutoshJha: It’s looping from `arr.length - 1` to `0`. The elements of `arr` are never involved. – Ry- Jan 09 '19 at 11:56
  • 1
    @Liam Not necessarily in some specific cases, the real performance difference is remarkable between for loop and a forEach loop. When there are nested loops and the array is big enough, several seconds can be saved. – Teemu Jan 09 '19 at 11:57
  • those several seconds are only useful if you decide that "looping" is a bottle neck, which I'd say 99% it won't be. This also smells of optimisation for optimisations sake, which just isn't worth investing time into. So yes in **specific** cases @Teemu but I don't see this as one of those. – Liam Jan 09 '19 at 11:58
  • @YuryTarabanko oh yeah typo , thanks correcting – Ashutosh Jha Jan 09 '19 at 11:59
  • 1
    @Teemu, last time I used jsperf (about a year ago) I found that the first test always runs slightly slower than subsequent tests, even when the code is identical. I don't know if this has improved... – garryp Jan 09 '19 at 12:09
  • @AshutoshJha Try running the snippet multiple times. You'll get different results depending on various reasons. Tests like this are usually worthless. You need to profile real code on real data bound to a target js engine. Though for-loop is usually faster on most engines, 200 items is definitely premature optimisation. – Yury Tarabanko Jan 09 '19 at 12:11

0 Answers0