Cache vs. Closure

Benchmark created by Milan Adamovsky on


Description

Check performance difference between caching dom elements vs. using a closure to achieve the same result.

Preparation HTML

<meta name="A" content="1" />
<meta name="B" content="2" />
<meta name="C" content="3" />
<meta name="D" content="4" />
<meta name="E" content="5" />
<script>
  var metaTags = document.getElementsByTagName("meta");
  
  function getMetaTags(x) {
   var metaTags = document.getElementsByTagName("meta");
   getMetaTags = function(y) {
    return metaTags[y];
   };
  
   return getMetaTags(x);
  }
  
  getMetaTags(); // we have to run this to equalize the tests.
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Caching
metaTags["A"];
metaTags["B"];
metaTags["C"];
metaTags["D"];
metaTags["E"];
ready
Closure
getMetaTags("A");
getMetaTags("B");
getMetaTags("C");
getMetaTags("D");
getMetaTags("E");
ready

Revisions

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

  • Revision 1: published by Milan Adamovsky on
  • Revision 2: published by Matt on