Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some quick tips for increasing jQuery performance? [closed]

What are some quick tips for increasing jQuery performance?

like image 788
Shaitender Singh Avatar asked Oct 19 '09 07:10

Shaitender Singh


People also ask

Does jQuery affect performance?

For small websites and simple web apps, the advantages of jQuery will often outweigh the minimal performance impact. For larger websites and web apps with thousands of lines of code, or those processing large amounts of data using JavaScript then performance is absolutely something to consider.

Which of this function is faster in jQuery?

Nearly all plain Javascript functions will be faster than jQuery operations. This is because jQuery has overhead in creating a jQuery object in order to be more flexible, allow for chaining, support collections, etc...

Does jQuery make DOM manipulation faster?

jQuery is a wrapper that normalizes DOM manipulation in a way that works consistently in every major browser. It's fully reasonable for it to perform 25x slower than direct DOM manipulation.


2 Answers

  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.
like image 99
G-Wiz Avatar answered Oct 10 '22 11:10

G-Wiz


Paul Irish recently did a presentation on performance at the jQuery Conference 2009. The slides are some of the most comprehensive that I have seen.

http://paulirish.com/perf/
http://www.slideshare.net/paul.irish/perfcompression

like image 36
Alex Sexton Avatar answered Oct 10 '22 10:10

Alex Sexton