Caching jQuery Objects

JavaScript performance comparison

Test case created by Todd


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=""></script>

<!--Sample DOM-->
<h1>Sample DOM</h1>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<div id="main">
  <div id="content">
  <div id="myDiv" class="myClass">
  <p>Sample content of non-consequence</p>
<a href="#">Terms</a>

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.

Testing in unknown unknown
Test Ops/sec
No Caching
Method Chaining
Object Caching
var $myDiv = $("#myDiv");
Object Caching + Chaining
var $myDiv = $("#myDiv");
No jQuery
var myDiv = document.getElementById("myDiv"); = "red"; = 1; = "block";
No jQuery (cssText)
var myDiv = document.getElementById("myDiv"),
    styles = ";color:red;opacity:1;display:block;"; += styles;

Compare results of other browsers


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:

1 comment

Sandeep commented :

Javascript is always fast.... These libraries just helps in better/readable code and cross browser compatibility... and easier effects also

Otherwise, JS is the way to go.....

Nice Test though....

Comment form temporarily disabled.

Add a comment