On Fri, Feb 18, 2011 at 8:00 AM, Peter van der Zee <[email protected]>wrote:
> The *only* real perf-wise *reason to cache* the length of the array you're
> iterating *is* for a *live-query* such as dom queries. For regular arrays,
> it really just is not worth the extra variable.
>
That's much too general a statement. With a large enough array of any kind,
it makes a difference. A case in point is my PolyGonzo project, which loops
through polygon arrays with tens of thousands of points.
In PolyGonzo I took one more step and stopped checking the length entirely,
using loops like this:
for( var element, i = -1; element = array[++i]; ) {
}
That made a small but noticeable difference too.
This loop form works because the arrays I'm working with are known to have
no null/false/undefined values. I believe it's the fastest way to loop
forward through an array of that nature.
If you repeat something enough times, small improvements can add up. :-)
Obviously the same goes for something like jquery's $foo.size(); or worse,
> $('#foo').size(). I'm only talking about the length of the array being
> iterated. But that should go without sayin...
>
jQuery's .size() method should never be used at all. It's a relic from the
very first versions of jQuery where the jQuery object was not an array-like
object with a .length property.
Instead of .size(), use the .length property.
-Mike
--
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]