9. Cache objects that you use a lot
var header = $(‘#header’);
var menu = header. nd(‘.menu’);
10. jQuery uses ‘right to left’ model
Example : Selecting all p tags inside #content
$(‘#content p’);
jQuery will start querying DOM for p tags and then look for #content
$(‘#content’). nd(‘p’); = The faster jQuery way
This method will be 20-30% faster
11. Write your own selectors
$.extend( $.expr[‘:’], {
myNewSelector: function(element)
{
// do something
}
});
$(‘#content :myNewSelector’);
15. Bind()
Most straight forward way to bind an event to an element
$(‘a’).bind(‘click’, function(e) { });
jQuery will bind the click event on all <a> elements
16. Live()
Will also bind elements that are added to DOM dynamically
$(‘a’).live(‘click’, function(e) { });
17. Delegate()
Delegate will bind the handler to a given element
instead of the document root
$(‘#content’).delegate(‘a’, ‘click’, function(e) { });
22. Always use object detection
if( $(‘#content’).length )
{
// do stuff
}
23. Use direct functions
The functions get() and post() are just shortcuts to ajax().
So calling ajax() will give you a small speed advantage.
24. Check out the utilities functions
$.inArray() Determine whether an element exists inside an array
$.isArray() Determine whether the argument is an array
$.map() Translate all items in an array or object to new array of items
$.support() Check for browser features
$.unique() Sorts an array of DOM elements, with the duplicates removed
...