Search code examples
javascripthtmljqueryperformanceweb

What are some quick tips for increasing jQuery performance?


What are some quick tips for increasing jQuery performance?


Solution

    1. Prefer simple selection first only by ID, and second only by tag name. Selecting by class name or CSS selector requires jQuery to walk the DOM, while ID and tag map to "native" browser DOM functions (getElementById and getElementByTagName).
    2. Cache your jQuery objects as much as possible.
    3. Scope your operations to a root jQuery object. Rather than selecting elements individually, select a common ancestor element and use the find function to find elements within the scope of that elements children. This is really only optimal if you are performing some operations on the common ancestor anyway; otherwise the overhead of finding the ancestor and caching it may outweigh the benefit of scoped traversal.
    4. Don't use $.each(), use for(;;) instead. It's over ten times faster.