Caching jQuery Results

Benchmark created on


Description

Tests the difference between caching jQuery results and not caching them.

Preparation HTML

<form id="form" action="">
  <div id="firstNameWrapper">
    <div><label for="firstName">First Name</label></div>
    <div><input type="text" id="firstName" name="firstName"></div>
  </div>
  <div id="lastNameWrapper">
    <div><label for="lastName">Last Name</label></div>
    <div><input type="text" id="lastName" name="lastName"></div>
  </div>
  <div id="mailListsWrapper">
    <div>Mailing Lists</div>
    <div>
      <label><input type="checkbox" id="mailingListOne" name="mailingListOne" class="mailingList" value="1"> One</label>
    </div>
    <div>
      <label><input type="checkbox" id="mailingListTwo" name="mailingListTwo" class="mailingList" value="1"> Two</label>
    </div>
    <div>
      <label><input type="checkbox" id="mailingListThree" name="mailingListThree" class="mailingList" value="1"> Three</label>
    </div>
  </div>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
  var cache = {
   $mailingLists: $('.mailingList')
  };
  
  function notCached() {
   var $firstName = $('#firstName'),
       $lastName = $('#lastName'),
       $mailingLists = $('.mailingList:checked');
  }
  
  function cached() {
   var $checkedLists = cache.$mailingLists.filter(':checked');
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
No Caching
notCached();
ready
Caching
cached();
ready

Revisions

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