[jQuery] Performance tip
[jQuery] Performance tip
This one's probably blindingly obvious to anyone with any significant
programming experience in javascript, but it's an easy one to forget
too.
Consider this code:
function fast ()
{
var myDivItems = $('#myDiv').children();
for (x = 0; x < 1000; x++)
{
myDivItems.length;
}
}
function slow ()
{
for (x = 0; x < 1000; x++)
{
$('#myDiv').children().length;
}
}
Both are functionally identical, but try profiling their run times in
Firebug!
Here's the average run times I got from running each function 5 times
fast: 3.125ms
slow: 3146.875ms
One more line of code, a bit more memory used, but huge win in loops.