Why cache jQuery selectors? (v5)

Revision 5 of this benchmark created on


Preparation HTML

<div id="bar"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Not cached
$(document).ready(function() {
  $("#bar").attr("style", "color: black;");
  $("#bar").attr("class", "foo");
  $("#bar").attr("class", "foo2");
  $("#bar").attr("class", "foo3");
  $("#bar").attr("class", "foo4");
});
ready
Cached
$(document).ready(function() {
  var foo = $("#bar");
  foo.attr("style", "color: black;");
  foo.attr("class", "foo");
  foo.attr("class", "foo2");
  foo.attr("class", "foo3");
  foo.attr("class", "foo4");
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.