lundi 2 octobre 2017

Is a simple `if` faster than `else if` when it breaks a loop?

This might be immeasurable, but I'll still ask. Out of curiosity.

In this code:

for (var i = 0; i < 10; i++) {
  if (i > 5) {
    break;
  }
  
  console.log("Do some stuff:", i);
}

I run a loop for 6 iterations and break it on the 7th.

In this code:

for (var i = 0; i < 10; i++) {
  if (i > 5) {
    break;
  } else {
    console.log("Do some stuff:", i);
  }
}

I do pretty much the same except the code that runs before the loop is broken is inside an else to the if that breaks it.

Obviously, the result is the same. However, are those two pieces of code processed differently at a lower level? Is one of them even slightly faster? Does it make any difference other than looks?

Aucun commentaire:

Enregistrer un commentaire