What are some quick tips for increasing jQuery performance?
-
7Buying a new computer would be the obvious way :-) – Joey Oct 19 '09 at 07:34
-
1I think you would find several duplicates to this question by using search. – Elzo Valugi Oct 19 '09 at 07:35
-
new computer!! :D, suggest him an OS as well :D – Rakesh Juyal Oct 19 '09 at 07:38
-
4Put racing stripes on the side of your PC, that always helps. – unrelativity Oct 19 '09 at 07:38
-
4http://stackoverflow.com/questions/182630/jquery-tips-and-tricks – rahul Oct 19 '09 at 07:38
-
1And paint it red! The red ones are the fastest! – soulmerge Oct 19 '09 at 07:40
-
1Duplicates - http://stackoverflow.com/questions/1405676/how-to-increase-performance-of-jquery, http://stackoverflow.com/questions/1411143/jquery-selector-performance, http://stackoverflow.com/questions/46214/good-ways-to-improve-jquery-selector-performance in fact there are many more searching with keywords "jQuery" and "performance" – Russ Cam Oct 19 '09 at 09:11
-
1@Johannes, you would have to buy a new computer to each of your users... – flybywire Oct 19 '09 at 09:30
-
which one is faster, the one with *racing stripes* or the one which is painted *red*. :o . Will it be much faster if Monu will have a new computer with red and white stripes? :o. – Rakesh Juyal Oct 19 '09 at 10:21
-
The one Jenson Button is using (; – Kieron Oct 19 '09 at 11:24
-
1lolz .. guys i have painted my system wid black n white strips . it still not working :) – Shaitender Singh Oct 19 '09 at 11:59
9 Answers
- 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).
- Cache your jQuery objects as much as possible.
- 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. - Don't use
$.each()
, usefor(;;)
instead. It's over ten times faster.

- 7,370
- 1
- 36
- 47
-
1
-
2Here is a for(;;) based version of one of the examples for $.each() from the jQuery documentation [ http://docs.jquery.com/Utilities/jQuery.each ] : for(var i = 0; i < arr.length; i++){ var id = arr[i]; $("#" + id).text("My id is " + id + "."); if (id == "four") break; } – G-Wiz Oct 19 '09 at 19:03
-
2Of course we all know a decremented while loop can perform even faster than that! The beauty of each is the context in which the function is called (e.g. this === your element). – Alex Sexton Oct 20 '09 at 22:01
-
Good points, Alex. I just learned about the decremented while loop optimization. Aside from cosmetics, what advantage exists with 'this'? – G-Wiz Oct 21 '09 at 01:31
-
1Its mostly just for ease of use, having context is an easy way to maintain consistency. More importantly, each() is still chainable, which is key in jquery's api. – Alex Sexton Oct 21 '09 at 16:17
-
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

- 392
- 1
- 12

- 10,401
- 2
- 29
- 41
Reference files on google's CDN so they load faster.

- 20,469
- 14
- 82
- 108
-
-
Wouldn't apply if you're developing on an intranet (users are in same building). Storing it on your own server would save them a trip to the internet. – Nathan Long Oct 19 '09 at 15:40
-
@Nathan on an intranet, this would be the kind of thing to be stored on a proxy and so save them a trip as well, but take your point and some places wouldn't have a proxy – dove Oct 20 '09 at 07:22
Rather than doing:
$("#foo").addClass('test');
$("#foo").removeClass("bar");
$("#foo").slideUp('slow');
you can do:
$("#foo").addClass('test').removeClass('bar').slideUp('slow');

- 261,656
- 265
- 575
- 769
-
-
3Or if you can't do it all at once, put $("#foo") in a variable and use that rather than recreating $("#foo") every time. – Chris Charabaruk Oct 19 '09 at 10:04
-
2@jpartogi: jQuery creates a new object every time you pass a selector into $() but because its methods return `this` you can stack them like that. The trick here is to not create three different objects all for the same selector. – Chris Charabaruk Oct 19 '09 at 10:05
it doeas apply always to common javascript: always cache, cache, cache e.g.:
var $this = $(this);
$this.css('width', parseInt($this.css('width')) + 20 + 'px');

- 17,913
- 6
- 34
- 52
It's not really possible to increase the speed of jQuery, it really depends on how efficient your code is, how efficient the browser's JS interpreter is and how quick the computer running the code is. You could perhaps try and rewrite jQuery to make it more efficient, but that's going to take time and it's most likely already quite optimised.

- 3,670
- 6
- 38
- 63
-
1True if you're looking at drastic improvements in speed. However, there are simple tricks which do make for better execution, even if its only 100 or so milliseconds. – Chris Charabaruk Oct 19 '09 at 10:08
One of the best ways to ensure efficiency is to make sure the *selector is targeting the element/class etc as specific as possible.
*$(SELECTOR)

- 24,079
- 20
- 92
- 147
Know when to use plain JavaScript instead of JQuery methods.
jQuery is an addition to JS+DOM, not a complete replacement for it. You don't have to use jQuery for every line of code you write. Some things are more succinctly expressed without it; many things are faster without it. Learn what the DOM makes available so you don't end up writing some of the sillier examples I've seen posted here.
eg.:
var ix= $('#'+selectname).children().index($('#'+selectname+' option:selected'));
faster, easier to read, doesn't break with unexpected characters in ID:
var ix= document.getElementById(selectname).selectedIndex;

- 528,062
- 107
- 651
- 834
-
2This can be condensed to: $('#someElement').find('option:selected'); – Alt-Rock Ninja Cowgirl Oct 19 '09 at 15:40
I think you asking for code optimization, but since the performance highly depends on the used JavaScript-Engine, I'd like to mention Google Chrome Frame.

- 62,884
- 17
- 92
- 129