3 ms·
For-loops like "for (i=0;i<myLinkCollection.length;i++)" should always be written like "for (var i=..."), unless there's an explicit "var i;" declaration somewh
by ramen 17y ago
For-loops like "for (i=0;i<myLinkCollection.length;i++)" should always be written like "for (var i=..."), unless there's an explicit "var i;" declaration somewhere. Otherwise, this creates a global variable "i", which can lead to some very confusing bugs if another function is called that also references the global "i".
- ams6110 17y agoBut don't make the mistake of assuming that when writing "for (var i=...)" that i is scoped to the loop block. JavaScript does not have block scope. It only has function scope, with the "Global Object" scope being the top level scope.
- maxwell 17y agoYou'll get better performance with for (var i=0, l=myLinkCollection.length; i<l; i++) { } http://robertnyman.com/2008/04/11/javascript-loop-performance/ http://robertnyman.com/2008/04/11/javascript-loop-performanc...
- WalkingDead 17y agoProbably first time I am seeing a micro-optimization having some really significant impact on a micro-benchmark test.