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 |4 Answers
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
版权声明:本文标题:performance - Javascript improved native for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738463230a2088152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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