Cache vs. Closure (v2)

Revision 2 of this benchmark created by Matt 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");
   return function(y) {
    return metaTags[y];
   };
  }
  
  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
var gmtFn = getMetaTags();

gmtFn("A");
gmtFn("B");
gmtFn("C");
gmtFn("D");
gmtFn("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