id vs class vs tag vs pseudo vs. attribute selectors

JavaScript performance comparison

Revision 2 of this test case created by Scott Kosman

Info

Comparing the performance difference of varied jQuery 1.7 selectors.

This test page contains some dummy HTML (from html-ipsum.com) just to give the page some weight, make the selectors work a little bit.

Note that the "Tag" selector is selecting a tag that happens to be unique in the page just to be fair.

Preparation code

<style type="text/css">
  .bar { display: none; }
</style>
<h1>
  HTML Ipsum Presents
</h1>

<p>
  <strong>
    Pellentesque habitant morbi tristique
  </strong>
  senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,
  feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam
  egestas semper.
  <em>
    Aenean ultricies mi vitae est.
  </em>
  Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra.
  Vestibulum erat wisi, condimentum sed,
  <code>
    commodo vitae
  </code>
  , ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros
  ipsum rutrum orci, sagittis tempus lacus enim ac dui.
  <a href="#">Donec non enim</a>
  in turpis pulvinar facilisis. Ut felis.
</p>

<h2>
  Header Level 2
</h2>
<ol>
  <li>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  </li>
  <li>
    Aliquam tincidunt mauris eu risus.
  </li>
</ol>
<blockquote id="foo" class="bar" name="baz">
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in
    mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa.
    Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus
    turpis elit sit amet quam. Vivamus pretium ornare est.
  </p>
</blockquote>
<h3>
  Header Level 3
</h3>
<ul>
  <li>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  </li>
  <li>
    Aliquam tincidunt mauris eu risus.
  </li>
</ul>
<pre>
  <code>
    #header h1 a { display: block; width: 300px; height: 80px; }
  </code>
</pre>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Preparation code output

HTML Ipsum Presents

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae , ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.

Header Level 2

  1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  2. Aliquam tincidunt mauris eu risus.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.

Header Level 3

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  
    #header h1 a { display: block; width: 300px; height: 80px; }
  

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery ID
$("#foo");
pending…
jQuery Tag
$("blockquote");
pending…
jQuery Class
$(".bar");
pending…
jQuery attribute
$("[name='baz']");
pending…
jQuery Pseudo-selector
$(":hidden");
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

7 comments

John Marty commented :

Why don't you add an example using $("blockquote").find("#foo") so we can see how much faster it is over using the id by itself. Its much faster, you should just have here for comparison.

Scott Kosman commented :

It is absolutely NOT faster, in fact it's slower than anything else besides the pseudo-selector.

See here: http://jsperf.com/id-vs-class-vs-tag-selectors/37

Terrible performance aside, there's absolutely no real-world use for that type of selector. Why would you need to limit the search scope when searching for an ID? In this case, you're making Sizzle run an extra search for all blockquotes in the body before searching that scope for #foo.

Simon East commented :

What about a showing a comparison between $('#id .class'); and $('#id').find('.class');

Joe commented :

what about combining some of these methods to narrow the scope? no bueno?

$('#foo p[name=bar]').watever;

6 commented :

4

剑桥 commented :

ID选择器首选之道!

Kent Liau commented :

how about data attributes?

$('[data-action=doSomethiing]'); $('button[data-action=doSomethiing]'); $('.btn[data-action=doSomethiing]');

Add a comment