Performance Zone is brought to you in partnership with:

A GNU/Linux g33k CLI+Web loving computer polyglot. Hemanth is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

How Much Does Caching Matter in JS?

09.18.2012
| 1738 views |
  • submit to reddit

Caching plays a very important role on both the client and server side. I tried to do a little test with benchmark.js, and caching proved to increase performance remarkably. In fact, the cache-less query was 98% slower than the cached query! Cache has its limitations with live objects, but it's worth a timely cache update in that case.

Below is the simple test code and some numbers from the test:

<div id="docs">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
<script>
  Benchmark.prototype.setup = function() {
    var divs = document.querySelectorAll('div#docs')
    var cache = {get: {'divs': divs}};
  };
</script>

Test code:

document.querySelectorAll('div#docs').length
 
cache.get.divs.length;

Results:

document.querSelectorAll('div#docs').length => 135,809 (ops) 98% slower.

cache cache.get.divs.length => 8,212,190 (ops) 0.99% faster.

Do let me know if you find a better way of speeding things up!

Published at DZone with permission of Hemanth Madhavarao, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)