There were a lot of good speakers at jQuery Summit, but Paul Irish‘s talk on jQuery Anti-Patterns for Performance & Compression was my stand-out favorite. Covering a number of advanced jQuery performance optimization techniques, this speech put my knowledge of jQuery optimization to shame.
Before Paul’s talk my understanding of jQuery performance tuning was fairly simplistic:
- Optimize selectors to descend from an id if possible.
- Use tag names when selecting classes and don’t use an excessive number of selectors.
- Define variables instead of selecting the same object repeatedly.
- Optimize your code replacing repetition with object oriented functions.
1. Optimize selectors for Sizzle’s ‘right to left’ model
As of version 1.3, jQuery has been using the Sizzle Javascript Selector Library which works a bit differently from the selector engine used in the past. Namely it uses a ‘right to left’ model rather than a ‘left to right’, which means that given a selector string such as$('div.class span.class2')
the engine starts the search on the right using the most specific selector (the span.class2
) and then checks each ancestor against div.class
rather than starting on the left and checking descendants.But what does that mean for you? Make sure that your right-most selector is really specific and any selectors on the left are more broad. For instance use:
$('.class span.class2')
rather than:$('div.class .class2')
And of course descending from an #id is still always best:$('#id div.class')
2. Use live() not click()
jQuery’s new event handlerlive()
is a much faster alternative to click()
. It comes with an additional benefit that I am extremely excited about: the selector works even with dynamic content, meaning no more reattaching click events every time you change a piece of content.Live is faster and better for all click events and most other jQuery event handling, however stay away from it for tracking onmouseover or onsubmit.
3. Pull elements off of the DOM while you play with them
Let’s say you have a form that you want to append a series of inputs to. Normally you might do this:var theForm = $('#myForm');
inputArr.each(function() {
theForm.append(this);
});
However it’s better to pull the form element off of the DOM while you play around with it:var theForm = $('#myForm');
var formParent = theForm.parent();
theForm.remove();
inputArr.each(function() {
theForm.append(this);
});
formParent.append(theForm);
4. Use find() rather than context
With jQuery selectors, context is good for performance, butfind()
is better. So instead of using context like this:var panels = $('div.panel', $('#content'));
It is better to use find()
:var panels = $('#content').find('div.panel');
More info on context
5. Use HTML 5
HTML 5 means more tags:
,
No comments:
Post a Comment