getElementById vs. getElementsByClassName vs. querySelector vs. jQuery vs xpath
JavaScript performance comparison
Info
Changes to rev 26 (rev 27 is worse):
- repositioning xpath
- remove foo + checks for it (search for selecting elem by id once is enough)
- remove getElementById("poo") in querySelector (Class) & jQuery Class Selector for a true comparison of the algorithms.
- jQuery Class Selector -> jQuery w. querySelector
- added real jQuery ClassSelector
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<p id="bar" class="my-bar">
Bar
</p>
<div id="poo" class="my-poo">
<div id="par" class="my-bar">
<div id="paz" class="my-bar">
<span id="baz" class="my-baz">
Baz
</span>
</div>
</div>
</div>
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… |
querySelector (ID) |
|
pending… |
jQuery ID Selector |
|
pending… |
xpath eval |
|
pending… |
getElementsByClassName |
|
pending… |
querySelector (Class) |
|
pending… |
jQuery ClassSelector |
|
pending… |
jQuery w. querySelector |
|
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 Ara Pehlivanian
- Revision 3: published
- Revision 4: published
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 8: published by vamp
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 14: published
- Revision 15: published by Name64
- Revision 16: published
- Revision 17: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 22: published
- Revision 23: published
- Revision 24: published
- Revision 25: published
- Revision 26: published
- Revision 27: published
- Revision 28: published by dc
- Revision 30: published
- Revision 31: published
- Revision 32: published
- Revision 33: published by AjiTae
- Revision 34: published by AjiTae
0 comments