Get Cookie Value

Benchmark created by Mac Heller-Ogden on


Setup

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 '';
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Get Cookie Value By Loop
getCookieValueByLoop('testCookie');
getCookieValueByLoop('anotherTestCookie');
ready
Get Cookie Value By Regex
getCookieValueByRegEx('testCookie');
getCookieValueByRegEx('anotherTestCookie');
ready
Get Cookie Value By Array Functions
getCookieValueByArrayFunctions('testCookie');
getCookieValueByArrayFunctions('anotherTestCookie');
ready

Revisions

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

  • Revision 1: published by Mac Heller-Ogden on