Cookie Parsing

JavaScript performance comparison

Revision 14 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js">
</script>
<script>
  function generateCookieString(count) {
    var name = "name",
        stringParts = [],
        array = new Array(6);
    for (var j = 0; j < count; j++) {
      stringParts.push(name + j + '=' + array.join(name + j));
    }
    return stringParts.join(';');
  }

  function string(name, cookie) {
    setPos = cookie.indexOf(name + '='), stopPos = cookie.indexOf(';', setPos);

    // Dataset does not exist, attempt to register default
    return !~setPos ? null : cookie.substring(
    setPos, ~stopPos ? stopPos : undefined).split('=')[1];
  }



  function regexp(cookieName) {
    var re = new RegExp('[; ]' + cookieName + '=([^\\s;]*)');
    var sMatch = (' ' + document.cookie).match(re);
    if (cookieName && sMatch) return unescape(sMatch[1]);
    return '';
  }

  function getStrCookie(name, cookie) {
    var regex = new RegExp("(^" + name + "=([a-zA-Z-_0-9]+);?.*)|(.*;\\s?" + name + "=([a-zA-Z-_0-9]+);?.*)"),
        ret
        
    return cookie.match(regex) ? cookie.replace(regex, "$2$4") : '';
  }

  function lawnchair(name, cookie) {
    var nameEQ = name + '=',
        ca = cookie.split(';'),
        len = ca.length,
        i = 0,
        c;

    for (; i < len; i++) {
      c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1, c.length);
      }
      if (c.indexOf(nameEQ) == 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return null;

  }

  function simplesplits(name, cookie) {

    var parts = cookie.split(name);

    return parts.length === 0 ? null : parts[1].split('=')[1].split(';')[0];

  }

  function CookieHandler() {
    function CookieParser() {
      var kvs = document.cookie.split(";");

      function trim(stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
      }

      function parseCookies() {
        var result = {};
        $.each(kvs, function(i, v) {
          var kvp = v.split("=");
          result[trim(kvp[0])] = kvp[1];
        });
        return result;
      }
      return {
        cookies: function() {
          return parseCookies();
        }
      };
    }

    return {
      getCookies: CookieParser().cookies,
      getCookie: function(cookieName) {
        return CookieParser().cookies()[cookieName] || null;
      }
    };
  }

  var cache = [];

  function cacheCookie(key, cookie) {
    if (!cache[key]) cookie.replace(/\b([^;=]*)=([^;]*)/g, function(m, k, v) {
      cache[k] = v
    });
    return cache[key]
  };
  var numberOfCookie = 30;
  var cookieString = generateCookieString(numberOfCookie);
</script>
    

Preparation code output

<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"> </script> <script> function generateCookieString(count) { var name = "name", stringParts = [], array = new Array(6); for (var j = 0; j < count; j++) { stringParts.push(name + j + '=' + array.join(name + j)); } return stringParts.join(';'); } function string(name, cookie) { setPos = cookie.indexOf(name + '='), stopPos = cookie.indexOf(';', setPos); // Dataset does not exist, attempt to register default return !~setPos ? null : cookie.substring( setPos, ~stopPos ? stopPos : undefined).split('=')[1]; } function regexp(cookieName) { var re = new RegExp('[; ]' + cookieName + '=([^\\s;]*)'); var sMatch = (' ' + document.cookie).match(re); if (cookieName && sMatch) return unescape(sMatch[1]); return ''; } function getStrCookie(name, cookie) { var regex = new RegExp("(^" + name + "=([a-zA-Z-_0-9]+);?.*)|(.*;\\s?" + name + "=([a-zA-Z-_0-9]+);?.*)"), ret return cookie.match(regex) ? cookie.replace(regex, "$2$4") : ''; } function lawnchair(name, cookie) { var nameEQ = name + '=', ca = cookie.split(';'), len = ca.length, i = 0, c; for (; i < len; i++) { c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; } function simplesplits(name, cookie) { var parts = cookie.split(name); return parts.length === 0 ? null : parts[1].split('=')[1].split(';')[0]; } function CookieHandler() { function CookieParser() { var kvs = document.cookie.split(";"); function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g, ""); } function parseCookies() { var result = {}; $.each(kvs, function(i, v) { var kvp = v.split("="); result[trim(kvp[0])] = kvp[1]; }); return result; } return { cookies: function() { return parseCookies(); } }; } return { getCookies: CookieParser().cookies, getCookie: function(cookieName) { return CookieParser().cookies()[cookieName] || null; } }; } var cache = []; function cacheCookie(key, cookie) { if (!cache[key]) cookie.replace(/\b([^;=]*)=([^;]*)/g, function(m, k, v) { cache[k] = v }); return cache[key] }; var numberOfCookie = 30; var cookieString = generateCookieString(numberOfCookie); </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
String
string('name15', cookieString);
pending…
RegExp
regexp('name15', cookieString);
pending…
Lawnchair
lawnchair('name15', cookieString);
pending…
Simple splits
simplesplits('name15', cookieString);
pending…
CookieHandler
CookieHandler().getCookie('name15');
pending…
$.cookie
$.cookie("name15")
pending…
getStrCookie
getStrCookie("name15", cookieString)
pending…
cached Cookie (1st call)
cache = [];
cacheCookie('name15', cookieString);
pending…
cached Cookie (all cookies)
for (var i = 0; i < numberOfCookie; i++) cacheCookie('name' + i, cookieString);
pending…
RegExp (all cookies)
for (var i = 0; i < numberOfCookie; i++) regexp('name' + i, cookieString);
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