I asked Thomas Lahn on comp.lang.javascript newsgroup about caching “length” property:

According to discussion here http://stackoverflow.com/questions/6261953/do-modern-javascript-jiters-need-array-length-caching-in-loops in your opinion property “length” still needs to be stored in variable before loop? Or this can be omitted because modern browsers caching (?) “length” property?

and he answered me:

One should never rely on what one cannot know. You cannot know the runtime
environments code once written will be exposed to. There is no good reason
to assume the value of the “length” property should be automatically cached
for a loop in the first place as it could change by statements in the loop.

One can know instead that avoiding to access a property repeatedly whose
value does not change, in favor of a local variable that holds this value,
reduces code complexity. That has a chance – and it has been showed – to
increase runtime efficiency, IOW to be faster. So it is wise to do that.

I must write that I strongly agree with above opinion. However, feel free to write constructive comment here.


You can leave a response, or trackback from your own site.

Before you add comment see for rules.

Leave a Reply

Your email address will not be published. Required fields are marked *