Get Cookie Value

JavaScript performance comparison

Revision 3 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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=/";
  
  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 getCookieValArray2(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
  }

};
</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…
Extra
getCookieValArray2('testCookie')
getCookieValArray2('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