Caching page data

JavaScript performance comparison

Test case created by Phillip Gourley

Info

Spike for storing page meta data inside an object to limit requests to the DOM to improve performance.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="basket">
</div>
<script>
Benchmark.prototype.setup = function() {
    var siteObj = {
      page: {
        type: 'basket'
      }
    }
    $('body').addClass('.basket');
    $body = $('body');
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
DOM request to retrieve data
// Not used cache selector to replicate bad practice.
if ($('body').hasClass('basket')) {
  return true;
}
pending…
Cached value to retrieve data
if (siteObj.page.type === 'basket') {
  return true;
}
pending…
DOM request to retrieve data (cached)
if ($body.hasClass('basket')) {
  return true;
}
pending…

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

Compare results of other browsers

0 comments

Add a comment