11

I have just installed Aptana Studio for development and one of the available commands for Javascript is Insert a for loop like this:

for (var i=0; i < Things.length; i++) {
  Things[i]
};

Another option is Insert improved for loop like this:

for (var i = Things.length - 1; i >= 0; i--){
  Things[i]
};

Why this last one is better than the first one?

  • 7
    Because it doesn't require accessing the length property each time. In the latter part, `for` initialisation is performed only once. BTW, most people would still prefer the first one for readability. – c.P.u1 Jul 05 '13 at 08:24
  • 1
    exact duplicate of [JavaScript - Are loops really faster in reverse...?](http://stackoverflow.com/questions/1340589/javascript-are-loops-really-faster-in-reverse) Please search yourself at first - and that question was #1 in the "Related" sidebar – Bergi Jul 05 '13 at 08:40
  • @c.P.u1 Or simply because they don't want to iterate their array backwards... – totymedli Jul 03 '16 at 14:37

4 Answers4

15
//   (  A  )  (       B       )  (C)
for (var i=0; i < Things.length; i++) {
  Things[i]
};
  • A is executed once before the loop starts.
  • B is re-evaluated before every iteration, and if it's not true, it exits the loop (hence it checks the length property of Things on every single iteration.)
  • C is executed after every iteration

That said, the performance you get from changing the loop is minimal, and you risk sacrificing part of the readability, so stick with what you find most readable - not what is most correct performance-wise.


This might make more sense for you:

for (var i=0; i < Things.length; i++) {
    Things[i] = null;
};

could be rewritten as the following:

var i = 0; //A
while (true) {
    if (!(i < Things.length)) { //B - We check the length all the time
        break;
    }
    Things[i] = null;
    i++; //C
}

and

for (var i = Things.length - 1; i >= 0; i--){
    Things[i] = null;
};

could be rewritten as the following:

var i = Things.length - 1; //A - We only check the length once
while (true) {
    if (!(i >= 0)) { //B
        break;
    }
    Things[i] = null;
    i--; //C
}
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • @Bergi Agreed - and it was thought of during the creation of this example code, but this is more of a showcase of what happens, and I personally believe that in this exact instance (for showing what it does), it makes more sense to show it by using a condition and a `break` statement. – h2ooooooo Jul 05 '13 at 10:12
  • I love the demonstration of different ways to write the same loop :-) – Rikki Apr 12 '14 at 23:04
5

Because the result of Things.length is not getting evaluated each time (on every iteration). Its just assigned once at the start and used from that point onwards. Other than that the number of iterations is obviously the same.

Its a micro-optimization really. You will find more interesting things to optimize in your code I presume.

Yannis
  • 6,047
  • 5
  • 43
  • 62
4

How about that?

for (var i = 0, len = things.length; i < len; i += 1) {
  // something
}

Update:

Sorry, English is not my mother language. So I have no idea how to start the conversation with you guys. Anyway, here are Youtube URL and I learned from that video. I hope this help you. It explains why!

https://www.youtube.com/watch?v=mHtdZgou0qU

Tony Jin
  • 59
  • 3
  • 1
    Although your answer might help, you should at least explain why. As it is, your answer has been passed into the Low Quality Posts moderation queue (which is where I am viewing it) I suggest you edit your answer to add an explanation. – Chris Spittles Feb 25 '16 at 09:26
1

I'm guessing that on the second one you only access Things.length once (when initializing i) where on the first one you access it every single time to check to see if you're there.

Adi H
  • 668
  • 7
  • 9