I know I was a little late on this topic, but I just wanted to know that over-packing DOM objects is one of the most common crimes against jQuery. Caring for the content of a jQuery instance can have amazing effects in terms of performance, and it's so easy to do that you have no excuse.
Usually, when people have a DOM object (referenced as this or element ), they will try to wrap it every time they need access to the jQuery method:
jQuery(this).css('a', 'b');
The problem is that you do this several times:
jQuery(this).css('a', 'b'); jQuery(this).find('span').attr(...); jQuery(this)....
Each time jQuery() is called, a new jQuery instance is created. Such an operation is expensive and should be avoided, if at all possible - especially in cycles!
To avoid this, for one, you could use a chain with all the methods that return an instance of jQuery $(this).css(a,b).attr(a,b)... ). The rest of the time, you should have a locally declared variable that references the jQuery instance, and then just use this:
var self = jQuery(this); self.css(...); self.attr(...);
If you do this in the .each() callback function, a new jQuery object is created at each separate iteration. You can avoid this by having one generic jQuery object that you constantly mutate, and then you can run jQuery methods from this single instance:
Look at this:
jQuery.single = function(a){ return function(b){ a[0] = b; return a } }(jQuery([1]));
Now look at this:
$('a').each(function(i){ $.single(this).append('Anchor number ' + i); });
Only one jQuery object is used. You can do this even faster by avoiding id resolution:
$_ = $.single; $('a').each(function(i){ $_(this).append('Anchor number ' + i); });
Food for thought. More details here : http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/