Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jQuery - Doing it right

appoosa
September 27, 2011

jQuery - Doing it right

appoosa

September 27, 2011
Tweet

More Decks by appoosa

Other Decks in Programming

Transcript

  1. Sizzle Selector engine used by jQuery created by John Resig.

    Now spun off to its own project – http://SizzleJS.com • Completely standalone (no library dependencies) • Competitive performance for most frequently used selectors • Only 4KB minified and gzipped • Highly extensible with easy-to-use API
  2. Writing better selectors Speed up performance with better queries •

    Be more specific on the right side query • ID selector rules everything • Use tag names in selectors • Don’t overdo the selectors
  3. Is the DOM ready? Do you need to wait until

    it is? Not everything needs to be done in $(document).ready(). $(document).ready(function () { / / D o s t u f f h e r e t h a t c a n b e d o n e o n l y a f t e r t h e D O M i s l o a d e d }); $("a").click(function () { / / D o o t h e r s t u f f h e r e });
  4. Cache is awesome Use it as often and as much

    as you can It helps limit the number of times the DOM needs to be traversed to fetch an element. And hence, significant performance improvement.
  5. “Scope” is fast. “Find” is faster. Doesn’t involve the sizzle

    engine. $(‘#parent').find(‘child'); -- is the same as -- $(‘child', $(‘#parent'));
  6. Event delegation Bind fewer events to the elements for better

    performance. $('#myList li).bind('click', function() { // do stuff }); $('#myList).bind('click', function(e){ var target = e.target; if (target.nodeName === 'LI') { // do stuff } });