<p>I'm looking for any way that I can improve the selector performance of a jQuery call.  Specifically things like this:</p>

<p>Is <code>$("div.myclass")</code> faster than <code>$(".myclass")</code></p>

<p>I would think it might be, but I don't know if jQuery is smart enough to limit the search by tag name first, etc.  Anyone have any ideas for how to formulate a jQuery selector string for best performance?</p>

[{'answer_id': 46240, 'author': 'Ryan Doherty', 'author_id': 956, 'author_profile': 'https://Stackoverflow.com/users/956', 'pm_score': 0, 'selected': False, 'text': "<p>I've been on some of the jQuery mailing lists and from what I've read there, they most likely filter by tag name then class name (or vice versa if it was faster). They are obsessive about speed and would use anything to gain a smidgen of performance.</p>\n\n<p>I really wouldn't worry about it too much anyway unless you are running that selector thousands of times/sec. </p>\n\n<p>If you are really concerned, try doing some benchmarking and see which is faster.</p>\n"}, {'answer_id': 46253, 'author': 'kamens', 'author_id': 1335, 'author_profile': 'https://Stackoverflow.com/users/1335', 'pm_score': 6, 'selected': True, 'text': '<p>There is no doubt that <strong>filtering by tag name first is much faster</strong> than filtering by classname.</p>\n\n<p>This will be the case until all browsers implement getElementsByClassName natively, as is the case with getElementsByTagName.</p>\n'}, {'answer_id': 218846, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>I'll add a note that in 99% of web apps, even ajax heavy apps, the connection speed and response of the web server is going to drive the performance of your app rather than the javascript.  I'm not saying the you should write intentionally slow code or that generally being aware of what things are likely to be faster than others isn't good.  </p>\n\n<p>But I am wondering if you're trying to solve a problem that doesn't really exist yet, or even if you're optimizing for something that might <em>change</em> in the near future (say, if more people start using a browser that supports <code>getElementsByClassName()</code> function referred to earlier), making your optimized code actually run slower. </p>\n"}, {'answer_id': 382663, 'author': 'Evan Moran', 'author_id': 47593, 'author_profile': 'https://Stackoverflow.com/users/47593', 'pm_score': 0, 'selected': False, 'text': '<p>Consider using Oliver Steele\'s Sequentially library to call methods over time instead of all at once.</p>\n\n<p><a href="http://osteele.com/sources/javascript/sequentially/#" rel="nofollow noreferrer">http://osteele.com/sources/javascript/sequentially/</a></p>\n\n<p>The "eventually" method helps you call a method after a certain period of time from its initial call.  The "sequentially" method lets you queue several tasks over a period of time.  </p>\n\n<p>Very helpful!</p>\n'}, {'answer_id': 382683, 'author': 'Evan Moran', 'author_id': 47593, 'author_profile': 'https://Stackoverflow.com/users/47593', 'pm_score': 2, 'selected': False, 'text': '<p>Another place to look for performance information is Hugo Vidal Teixeira\'s Performance analysis of selectors page.  </p>\n\n<p><a href="http://www.componenthouse.com/article-19" rel="nofollow noreferrer">http://www.componenthouse.com/article-19</a></p>\n\n<p>This gives a good run down of speeds for selector by id, selector by class, and selector prefixing tag name.</p>\n\n<p>The fastest selectors by id was:  $("#id")</p>\n\n<p>The fastest selector by class was:  $(\'tag.class\')  </p>\n\n<p>So prefixing by tag only helped when selecting by class!  </p>\n'}, {'answer_id': 382730, 'author': 'Matthew Crumley', 'author_id': 2214, 'author_profile': 'https://Stackoverflow.com/users/2214', 'pm_score': 4, 'selected': False, 'text': '<p>In some cases, you can speed up a query by limiting its context. If you have an element reference, you can pass it as the second argument to limit the scope of the query:</p>\n\n<pre><code>$(".myclass", a_DOM_element);\n</code></pre>\n\n<p>should be faster than</p>\n\n<pre><code>$(".myclass");\n</code></pre>\n\n<p>if you already have a_DOM_element and it\'s significantly smaller than the whole document.</p>\n'}, {'answer_id': 416144, 'author': 'jQuery Lover', 'author_id': 49200, 'author_profile': 'https://Stackoverflow.com/users/49200', 'pm_score': 2, 'selected': False, 'text': '<p>Here is how to icrease performance of your jQuery selectors:</p>\n\n<ul>\n<li>Select by #id whenever possible (<a href="http://jquery-howto.blogspot.com/2009/01/improving-jquery-code-performance.html" rel="nofollow noreferrer">performance test results</a> ~250 faster)</li>\n<li>Specify scope of your selections (<code>$(\'.select\', this)</code>)</li>\n</ul>\n'}, {'answer_id': 858596, 'author': 'Dmitri Farkov', 'author_id': 103647, 'author_profile': 'https://Stackoverflow.com/users/103647', 'pm_score': 3, 'selected': False, 'text': "<p>In order to fully comprehend what is faster, you have to understand how the CSS parser works.</p>\n\n<p>The selector you pass in gets split into recognizable parts using RegExp and then processed piece by piece.</p>\n\n<p>Some selectors like ID and TagName, use browser's native implementation which is faster. While others like class and attributes are programmed in separately and therefore are much slower, requiring looping through selected elements and checking each and every class name.</p>\n\n<p>So yes to answer your question:</p>\n\n<p>$('tag.class') is faster than just $('.class'). Why?\nWith the first case, jQuery uses the native browser implementation to filter the selection down to just the elements you need. Only then it launches the slower .class implementation filtering down to what you asked for.</p>\n\n<p>In the second case, jQuery uses it's method to filter each and every element by grabbing class.</p>\n\n<p>This spreads further than jQuery as all javascript libraries are based on this. The only other option is using xPath but it is currently not very well supported among all browsers.</p>\n"}, {'answer_id': 2061997, 'author': 'Alex Angas', 'author_id': 6651, 'author_profile': 'https://Stackoverflow.com/users/6651', 'pm_score': 0, 'selected': False, 'text': '<p>A <a href="https://stackoverflow.com/questions/2061188/problem-with-contains-selector-in-ie8/2061366#2061366">great tip</a> from a question I asked: Use <strong>standard CSS selectors</strong> wherever possible. This allows jQuery to use the <a href="http://dev.w3.org/2006/webapi/selectors-api/" rel="nofollow noreferrer">Selectors API</a>. According to <a href="http://ejohn.org/blog/queryselectorall-in-firefox-31/" rel="nofollow noreferrer">tests performed by John Resig</a>, this results in near-native performance for selectors. Functions such as <code>:has()</code> and <code>:contains()</code> should be avoided.</p>\n\n<p>From my research support for the Selectors API was introduced with jQuery 1.2.7, Firefox 3.1, IE 8, Opera 10, Safari 3.1.</p>\n'}, {'answer_id': 2062027, 'author': 'Reid', 'author_id': 236139, 'author_profile': 'https://Stackoverflow.com/users/236139', 'pm_score': 0, 'selected': False, 'text': "<p>If I am not mistaken, jQuery also is a bottom up engine. That means <code>$('#foo bar div')</code> is a lot slower than <code>$('bar div #foo')</code>. For example, <code>$('#foo a')</code> will go through all of the <code>a</code> elements on the page and see if they have an ancestor of <code>#foo</code>, which makes this selector immensely inefficient. </p>\n\n<p>Resig may have already optimized for this scenario (it wouldn't surprise me if he did - I believe he did in his Sizzle engine, but I am not 100% certain.) </p>\n"}, {'answer_id': 2255881, 'author': 'Lawrence', 'author_id': 272206, 'author_profile': 'https://Stackoverflow.com/users/272206', 'pm_score': 0, 'selected': False, 'text': '<p>I believe that selecting by ID first is always faster:</p>\n\n<pre><code>$("#myform th").css("color","red");\n</code></pre>\n\n<p>should be faster than</p>\n\n<pre><code>$("th").css("color","red");\n</code></pre>\n\n<p>However, I wonder how much chaining helps when starting with the ID? Is this</p>\n\n<pre><code>$("#myform").find("th").css("color","red")\n.end().find("td").css("color","blue");\n</code></pre>\n\n<p>any faster than this?</p>\n\n<pre><code>$("#myform th").css("color","red");\n$("#myform td").css("color","blue");\n</code></pre>\n'}, {'answer_id': 4113652, 'author': 'Philipp', 'author_id': 499255, 'author_profile': 'https://Stackoverflow.com/users/499255', 'pm_score': 3, 'selected': False, 'text': "<p>As Reid stated above jQuery is working from the bottom up. Although </p>\n\n<blockquote>\n  <p>that means <code>$('#foo bar div')</code> is a\n  lot slower than <code>$('bar div #foo')</code></p>\n</blockquote>\n\n<p>That's not the point. If you had <code>#foo</code> you wouldn't put anything before it in the selector anyway since IDs have to be unique.</p>\n\n<p>The point is:</p>\n\n<ul>\n<li>if you are subselecting anything from an element with an ID then select the later first and then use <code>.find</code>, <code>.children</code> etc.: <code>$('#foo').find('div')</code></li>\n<li>your leftmost (first) part of the selector <em>can</em> be less efficient scaling to the rightmost (last) part which <em>should</em> be the most efficient - meaning if you don't have an ID make sure you are looking for <code>$('div.common[slow*=Search] input.rare')</code> rather than <code>$('div.rare input.common[name*=slowSearch]')</code> - since this isn't always applicable make sure to force the selector-order by splitting accordingly.</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/46214', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4541/']