Caching jQuery Objects
JavaScript performance comparison
Info
This test compares the performance impact of three different jQuery scenarios:
1) Multiple selectors, multiple statements 2) Single selector, chained statements 3) Cached selector, multiple statements
The goal is to identify the best way to handle jQuery selectors for optimum performance.
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<!--Sample DOM-->
<header>
<h1>
Sample DOM
</h1>
</header>
<nav>
<ul>
<li>
Item 1
</li>
<li>
Item 2
</li>
<li>
Item 3
</li>
</ul>
</nav>
<div id="main">
<div id="content">
<div id="myDiv" class="myClass">
<p>
Sample content of non-consequence
</p>
</div>
</div>
</div>
<footer>
<a href="#">Terms</a>
</footer>
Preparation code output
Sample DOM
Sample content of non-consequence
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
No Caching |
|
pending… |
Method Chaining |
|
pending… |
Object Caching |
|
pending… |
Object Caching + Chaining |
|
pending… |
No jQuery |
|
pending… |
No jQuery (cssText) |
|
pending… |
jQuery using object. |
|
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 Todd
- Revision 2: published
- Revision 3: published by Krustev
- Revision 4: published
- Revision 6: published by Steponas
- Revision 7: published
- Revision 8: published by Jon z
- Revision 9: published
- Revision 10: published by Ray Baker
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published by Luca
- Revision 15: published by Todd Anglin
- Revision 16: published
- Revision 17: published by Thiago Braga
0 comments