admin管理员组

文章数量:1194735

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?

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?

Share Improve this question asked Jul 5, 2013 at 8:18 user2551602user2551602 3
  • 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 Commented Jul 5, 2013 at 8:24
  • 1 exact duplicate of JavaScript - Are loops really faster in reverse...? Please search yourself at first - and that question was #1 in the "Related" sidebar – Bergi Commented Jul 5, 2013 at 8:40
  • @c.P.u1 Or simply because they don't want to iterate their array backwards... – totymedli Commented Jul 3, 2016 at 14:37
Add a comment  | 

4 Answers 4

Reset to default 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
}

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.

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

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.

本文标签: performanceJavascript improved native for loopStack Overflow