Get Cookie Value

JavaScript performance comparison

Revision 9 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  document.cookie="lipsum=Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";
  document.cookie="testCookie=Foo Bar; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";
  document.cookie="anotherTestCookie=Baz Qux; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";
  document.cookie="unusedCookie=norf norf; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";
  function getCookieValueByRegEx(a, b) {
      b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
      return b ? b.pop() : '';
  }
  
  function getCookieValueByArrayFunctions(a, b, c) {
      b = '; ' + document.cookie;
      c = b.split('; ' + a + '=');
      return !!(c.length - 1) ? c.pop().split(';').shift() : '';
  };
  
  function getCookieValueByLoop(a, b, c, d, e) {
      b = document.cookie.split('; ');
      for (e = b.length - 1; e >= 0; e--) {
         c = b[e].split('=');
         if (a === c[0]) return c[1];
      }
      return '';
  }
  
  function getCookieValueByLoopRegEx(a) {
    var cookies = document.cookie.split(/[=;]\s*/);
          for(var i = cookies.length; i >= 0; i -= 2) {
              if(cookies[i] === a) {
                  return cookies[i + 1];
              }
          }
  }

};
</script>

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
Get Cookie Value By Regex
getCookieValueByRegEx('testCookie');
getCookieValueByRegEx('anotherTestCookie');
pending…
Get Cookie Value By Array Functions
getCookieValueByArrayFunctions('testCookie');
getCookieValueByArrayFunctions('anotherTestCookie');
pending…
Get Cookie Value By Loop
getCookieValueByLoop('testCookie');
getCookieValueByLoop('anotherTestCookie');
pending…
getCookieValueByLoopRegEx
getCookieValueByLoopRegEx('testCookie');
getCookieValueByLoopRegEx('anotherTestCookie');
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