Writing efficient jQuery code
Let's now proceed and analyze the most important jQuery-specific performance tips. For more information about the most up-to-date performance tips on jQuery, keep an eye on the relevant page for jQuery's Learning Center: http://learn.jquery.com/performance
Minimizing DOM traversals
Since jQuery made DOM traversals so simple, many web developers overused the $()
function everywhere, even in subsequent lines of code, making their implementations slower by executing unnecessary code. One of the main reasons that the complexity of the operation is so often overlooked is the elegant and minimalistic syntax that jQuery uses. Despite the fact that JavaScript browser engines became many times faster in the last few years, with performance comparable to many compiled languages, the DOM API is still one of their slowest components and, as a result, developers have to minimize their interactions with it.
Caching jQuery objects
Storing the result of the $()
function...