getCookie performance

Benchmark created by simonBorn on


Setup

var cookieString = "areTheseCookiesForReal=a bit yes, a bit no:); selectedLocale_DE=de-DE; BigBlipServerGateway.exampledomain.net-80=!+3/DoyBgIeuOiOoioLRblaknbnahdk34LTkZhjNCEgNctCaAttzcGOEm0Ece+QyTVW6KtHgo6MqMGMLED6VJh6n1DSmRsU1tF5F4=; toolEndUserId=aei135070970193984r0.819135799384134; anonymousUserId=551089EE-3462-495F-A4B8-6ED8FB02E52C; s_ien=5020475FAF74E472-0944148AF348CAC23B11; s_cc=true; s_vi=[CS]v1|2E4872BAD3B95A85317B6B-6000012C2000D38C[CE]; _gcl_au=1.1.394234234094.15135135305; _ga=GA1.2.9671134119.1548185135; UserSettings=SelectedStore=eb95829c-2b13-4d7b-876b-44b939d00e95; allowedCookieCategories=necessary%7Cnecessary%7Cfunctional%7Cperformance%7Cpromotional%7CUncategorized; AMCVS_38A468F454E7761B0A4C98A6%40AdobeOrg=1; s_sq=%5B%5BB%5D%5D; _gid=GA1.2.514664361.1548670195; _fbasp=fb.1.1548134670195426.550304453347; AMCV_38A468F454E77ASE74361B0A4C98A6%40AdobeOrg=690614123%7CMCIDTS%7C17925%7CMCMID%7C66367474877825659290195028956090233947%7CMCAAMLH-154952525278582%7C6%7CMCAAMB-15492723428582%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCOPTOUT-1548680982s%7CNONE%7CvVersion%7C3.1.0; s_nr=1548675523443280414-Repeat; gpv_padn=no%20value; cookieWithNoValue; cookieWithSpaces= This is a test";
      
      
      var identsToCheck = ["gpv_padn","fooNotInCookie","_ga","allowedCookieCategories"];

Test runner

Ready to run.

Testing in
TestOps/sec
Classic function
function getCookie(cname) {
    var name = cname + "=";
    var ca = cookieString.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

identsToCheck.forEach(function(el){
   var test = getCookie(el);
});
ready
Using RegEx
function getCookie(name) {
   var match = cookieString.match(RegExp('(^| )' + name + '=([^;]+)'));
   return match ? match[2] : ''
}

identsToCheck.forEach(function(el){
   var test = getCookie(el);
});
ready
Using RegEx with word boundary
function getCookie(name) {
    var r = cookieString.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : null;
};

identsToCheck.forEach(function(el){
   var test = getCookie(el);
});
ready
Using split
function getCookie(name) {
	var value = "; " + cookieString;
	var parts = value.split("; " + name + "=");
	if (parts.length == 2) return parts.pop().split(";").shift(); return "";
	}

identsToCheck.forEach(function(el){
   var test = getCookie(el);
});
ready

Revisions

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

  • Revision 1: published by simonBorn on