Selector Specificity Comparison
JavaScript performance comparison
Info
Comparison between several different methods of querying the DOM.
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="idDiv">
<ul>
<li></li>
<li class="selected"></li>
<li></li>
</ul>
</div>
<div class="classDiv">
<ul>
<li></li>
<li class="selected"></li>
<li></li>
</ul>
</div>
<script>
Benchmark.prototype.setup = function() {
var idDiv = $('div#idDiv');
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
getElementById |
|
pending… |
jQuery ID Selector |
|
pending… |
jQuery Specific ID Selector |
|
pending… |
jQuery Class Selector |
|
pending… |
jQuery Specific Class Selector |
|
pending… |
getElementsByClassName |
|
pending… |
Nested jQuery Strong Left |
|
pending… |
Nested jQuery Strong Right |
|
pending… |
Nested jQuery Strong Both |
|
pending… |
Nested jQuery Non-Cache Context |
|
pending… |
Nested jQuery Cached Context |
|
pending… |
querySelector ID |
|
pending… |
querySelector Specific ID |
|
pending… |
querySelector Class |
|
pending… |
querySelector Specific Class |
|
pending… |
querySelectorAll > li |
|
pending… |
jQuery > li |
|
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 by Tyler Wood
- Revision 2: published by Tyler Wood
0 comments