id vs class vs tag vs pseudo vs. attribute selectors
JavaScript performance comparison
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
- Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
- 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.
| Test | Ops/sec | |
|---|---|---|
jQuery ID |
|
pending… |
jQuery Tag |
|
pending… |
jQuery Class |
|
pending… |
jQuery attribute |
|
pending… |
jQuery Pseudo-selector |
|
pending… |
jQuery ID with Pseudo-selector |
|
pending… |
jQuery ID with psuedo-selector in find method |
|
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:
- Revision 1: published
- Revision 2: published by Scott Kosman
- Revision 3: published
- Revision 4: published
- Revision 5: published by Jozef
- Revision 6: published
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published by Rob
- Revision 11: published
- Revision 12: published by Ben Plum
- Revision 13: published
- Revision 14: published
- Revision 15: published
- Revision 16: published by Felipe
- Revision 17: published by Chris McKee
- Revision 18: published by Mohnish Thallavajhula
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 22: published
- Revision 23: published
- Revision 24: published by Lennon Carvalho
- Revision 26: published by Levi
- Revision 27: published
- Revision 28: published
- Revision 29: published
- Revision 30: published
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published by fafdsfdsgds
- Revision 36: published
- Revision 37: published by Scott Kosman
- Revision 38: published
- Revision 39: published
- Revision 40: published
- Revision 41: published
- Revision 42: published
- Revision 43: published by PeterOak
- Revision 44: published by PeterOak
- Revision 45: published by leo
- Revision 46: published
- Revision 47: published
- Revision 48: published by Eamon Nerbonne
- Revision 49: published
- Revision 50: published
- Revision 51: published
- Revision 52: published by Todd Anglin
- Revision 53: published
- Revision 54: published by Sujay
- Revision 55: published by Emilis
- Revision 57: published
- Revision 59: published
- Revision 60: published
- Revision 61: published by Anas Nakawa
- Revision 64: published
- Revision 68: published
- Revision 69: published
- Revision 70: published
- Revision 74: published
- Revision 76: published by Todd Anglin
- Revision 77: published
- Revision 78: published
- Revision 79: published
- Revision 80: published
- Revision 81: published
- Revision 82: published
- Revision 83: published
- Revision 84: published
- Revision 87: published
- Revision 88: published
- Revision 89: published
- Revision 92: published
- Revision 93: published
- Revision 94: published
- Revision 95: published
- Revision 96: published by gb
- Revision 98: published
- Revision 99: published
- Revision 100: published by Rain Kesküla
- Revision 101: published by Beau Smith
- Revision 102: published
- Revision 103: published
- Revision 104: published by Dmitriy
- Revision 105: published
- Revision 106: published
- Revision 107: published
- Revision 108: published
- Revision 110: published by With JQ
- Revision 111: published
- Revision 112: published
- Revision 113: published
- Revision 114: published
- Revision 118: published
- Revision 120: published
- Revision 121: published
- Revision 122: published by Tim Pietrusky
- Revision 123: published
- Revision 124: published
- Revision 125: published by snekse
- Revision 126: published by Puah Hock Leng
- Revision 127: published
- Revision 128: published
- Revision 129: published by eric
0 comments