Created
September 29, 2010 16:20
-
-
Save getify/603041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var i = 0, j = foo.length; i < j; i++) { | |
// do stuff | |
} | |
for (var i = foo.length; i;) { | |
// first usage in loop: blah[--i] | |
// do stuff slightly faster in reverse order | |
} |
also, i didn't claim it was better/cleaner syntactically. it is shorter, and at least as fast if not faster. but any such for-loop optimizations have to be taken with the grain of salt that the code will only be written and maintained by people who understand such syntax.
just updated again to fix minor issues.
http://jsperf.com/for-loop-test/4
Again, the last test is performing almost the same as the others, and sometimes 1% faster. definitely not statistically slower.
That might be the case in the particular version of Chrome 6 you were using to test. In the tests I performed, however, there was a greater than 1% difference.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it wasn't slowest for me, it was 1% faster in the chrome6 test i initially ran. it definitely wasn't the 20% slower that your revision had.