Caching $(this) in jQuery

Benchmark created by vzwick on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="foo">
  <div class="foobar">
    <a href="http://google.de">To Google!</a>
  </div>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
Uncached
$('div.foo').find('a').addClass('google');
$('div.foo').find('div').toggleClass('foobar');
$('div.foo').find('div a.google').addClass('baz');
ready
Cached
$this = $('div.foo');
$this.find('a').addClass('google');
$this.find('div').toggleClass('foobar');
$this.find('div a.google').addClass('baz');
ready

Revisions

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