On Tue, Dec 21, 2010 at 20:54, jemptymethod <[email protected]> wrote: > What is "out-smarting" the compiler, really? I often get criticized > for the following style of for loop: > > for (var i=0, n=arr.length; i<n; i++) > > On the grounds that I'm trying to outsmart the compiler. I'm > certainly not trying to do that. Rather I'm formulating a legal for > loop, *and* obviating a property access on every iteration, because > the language spec tells me the second chunk will get executed each > time. > > If a compiler barfs on the above, shame on whoever wrote the compiler.
Why do that when the compiler does it for you, and probably in a better way? It's totally reasonable for a compiler to try to optimize a normal for-loop written like for (i = 0; i < arr.length; i++) // where i is defined somewhere by a var statement because it's the most used form of for-loops. Just for example, it could check _very specifically_ if the second part of the statement is of the form "X < Y.length", and if so, cache the length of the array _internally_. It might even be faster than you doing the caching manually in that your way might introduce more overhead involving eg. creating symbol table entries. I'm not sure about that, but I'm just trying to prove that you consciously doing some sort of optimization does not necessarily result in better performance than not doing it. P.S. please use arr.forEach. ======================= 杨旭东 YANG Xudong "Wyverald" Twitter <http://twitter.com/Wyverald> - Tumblr <http://wyverald.tumblr.com/>- GitHub <https://github.com/Wyverald> - Douban<http://www.douban.com/people/45985185/> Juntos, unidos, triunfará nuestro deseo de ser el mejor alias please=sudo please rm -rf / On Tue, Dec 21, 2010 at 20:54, jemptymethod <[email protected]> wrote: > On Dec 19, 4:48 am, Szymon Piłkowski <[email protected]> > wrote: > > Hello, > > > > So, we've got new javascript engines (v8/jagermonkey), which will use > > JIT compilers to do their magic and optimise performance of our core. > > The question is: should we still use our own magic to do the same job, > > or should we start being nice to the compilers and leave such problems > > to them? > > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected]<jsmentors%[email protected]> > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
