DOM Caching

JavaScript performance comparison

Revision 2 of this test case created by Sam Deering

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="container">
  <ul>
    <li>
      list
    </li>
    <li>
      <ul>
        <li class="nested">
          nested 1
        </li>
        <li class="nested">
          nested 2
        </li>
        <li class="nested">
          nested 3
        </li>
      </ul>
    </li>
    <li>
      list
    </li>
    <li>
      list
    </li>
    <li>
      list
    </li>
  </ul>
  <div id="status">
  </div>
</div>
    

Preparation code output

<div id="container"> <ul> <li> list </li> <li> <ul> <li class="nested"> nested 1 </li> <li class="nested"> nested 2 </li> <li class="nested"> nested 3 </li> </ul> </li> <li> list </li> <li> list </li> <li> list </li> </ul> <div id="status"> </div> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
cache created individually
MY_OBJECT = {
  cache: {},

  init: function() {
    this.cache.c = $('#container');
    this.cache.n = $('.nested');
    this.cache.s = $('#status');
    this.cache.l = $('#container li').eq(0);
  },

  "run": function() {
    //cache class elements loop
    $.each(MY_OBJECT.cache.n, function(i, v) {
      //...
    });
  }
}

MY_OBJECT.init();
pending…
cache created by container
MY_OBJECT = {
  cache: {},

  init: function() {
    this.cache.c = $('#container');
    this.cache.n = this.cache.c.find('.nested');
    this.cache.s = this.cache.c.find('#status');
    this.cache.l = this.cache.c.find('li').eq(0);
  },

  "run": function() {
    //cache class elements loop
    $.each(MY_OBJECT.cache.n, function(i, v) {
         //...
    });
  }
}

MY_OBJECT.init();
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.

0 Comments