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

jQuery Performance Tips

jQuery Performance Tips

Tips to improve performance jQuery

Kunto Aji. K

August 15, 2014
Tweet

More Decks by Kunto Aji. K

Other Decks in Technology

Transcript

  1. Javascript & jQuery Javascript: an object-oriented computer programming language commonly

    used to create interactive effects within web browsers. jQuery: jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers
  2. Why Does Performance Matter? Bad code = browsers can end

    up having to do more work = more memory use = slower apps
  3. #2: Caching Cache Selector ~> Storing the result of selection

    for later use. var my_div = $(“#my-div”); ~> Each $(‘...’) always re-run searching ~> Caching will decrease repeat selections
  4. #2: Caching (3) Cache length ~> Storing the result of

    length. Preventing to access length property of an array every time
  5. #3: ID, Element, & Class • $(“#my-div”) -> document.getElementsById(“my-div”); •

    $(“div”) -> document.getElementsByTagName(“div”); • $(“.my-class”) -> document.getElementsByClassName (“.my-class”);
  6. #4: Use .find() (2) ~> $(list).find('.test'); // -> fastest $('.test',

    list); ~> translate to $(list).find('.test');
  7. #4: Use .find() (3) $('#list').find('.test') ~> parent is jQuery object

    ~> slower than using document. getElementByID $('.test', $('#list')) ~> translate to $('#list').find('.test')
  8. #4: use .find() (4) $('#list .test'); ~> jQuery parser works

    from right to left ~> search .test first before #list ~> internally use .find()
  9. #4: use .find() (5) $('#list > .test') ~> search .test

    first before checking direct child. $(list).children('.test') ~> internaly use $.sibling and Javascript nextSibling()
  10. #5: Use Fewer Selectors http://jsperf.com/specific-jquery-selector/2 ~> Use tag.class if possible

    on your right-most selector, and just id, tag or just .class on the left.