querySelectorAll vs getElementsByTagName vs Hybrid
JavaScript performance comparison
Info
Compare document.querySelectorAll vs document.getElementsByTagName vs a hybrid approach that uses the one fastest in the given browser.
Looks at all elements and pulls out its length to force getElementsByTagName to dereference the live nodeList.
Preparation code
<div>
<ul>
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
<li><a href="#">item 3</a></li>
</ul>
</div>
<form>
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
</form>
<div>
<p>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
</p>
<p>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
</p>
</div>
<script>
Benchmark.prototype.setup = function() {
var f=(/Opera/.test(navigator.userAgent)?document.querySelectorAll:document.getElementsByTagName);
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
querySelectorAll |
|
pending… |
getElementsByTagName |
|
pending… |
hybrid |
|
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 Andrew Hedges
- Revision 17: published
- Revision 20: published by Jeff Larson
- Revision 21: published by Sebastian Poreba
- Revision 22: published by MONTILLET
- Revision 25: published
- Revision 26: published
- Revision 27: published by Philip Tellis
- Revision 28: published by Carl Letourneau
- Revision 29: published
- Revision 30: published by Philip Tellis
- Revision 32: published
- Revision 33: published
- Revision 34: published
- Revision 35: published
- Revision 36: published
- Revision 37: published by Derp
- Revision 38: published
- Revision 39: published
- Revision 40: published by Zebulon84
- Revision 41: published by Jim Montgomery
- Revision 42: published by Jim Montgomery
- Revision 43: published
- Revision 44: published by Jim Montgomery
- Revision 45: published
- Revision 46: published
- Revision 47: published
- Revision 48: published by Adam
0 comments