Editing Cookie Parsing This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Comparing two RegExp and String parsing methods for getting a value from a cookie. Then adding in Lawnchair's cookie parser, for fun. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="//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 = 1; 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(name, cookie) { var regex = new RegExp(name + '=([^;]*)', 'g'), result = regex.exec(cookie); return result[1] || null; } function getStrCookie(name) { var regex = new RegExp("(^" + name + "=([a-zA-Z-_0-9]+);?.*)|(.*;\\s?" + name + "=([a-zA-Z-_0-9]+);?.*)"), cookie = document.cookie; 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 cookieString = generateCookieString(30); </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code string('name15', cookieString); Test 2 Title Async (check if this is an asynchronous test) Code regexp('name15', cookieString); Test 3 Title Async (check if this is an asynchronous test) Code lawnchair('name15', cookieString); Test 4 Title Async (check if this is an asynchronous test) Code simplesplits('name15', cookieString); Test 5 Title Async (check if this is an asynchronous test) Code CookieHandler().getCookie('name15'); Test 6 Title Async (check if this is an asynchronous test) Code $.cookie("name15") Test 7 Title Async (check if this is an asynchronous test) Code getStrCookie("name15")