Cookie Parsing

JavaScript performance comparison

Test case created by BrianMB

Info

Comparing two RegExp and String parsing methods for getting a value from a cookie.

Then adding in Lawnchair's cookie parser, for fun.

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
String
(function() {
 var set = 'name2',
     cookie = 'name1=val1;name2=val2;name3=val3',
     setPos = cookie.indexOf(set + '='),
     stopPos = cookie.indexOf(';', setPos);

 // Dataset does not exist, attempt to register default
 return !~setPos ? null : cookie.substring(
 setPos, ~stopPos ? stopPos : undefined).split('=')[1];
})();
pending…
RegExp
(function() {
 var set = 'name2';
 var cookie = 'name1=val1;name2=val2;name3=val3';
 var regex = new RegExp(set + '=([^;]*)', 'g');
 var result = regex.exec(cookie);
 return result[1] || null;
})();
pending…
Lawnchair
(function() {
 var name = 'name2';
 var cookie = 'name1=val1;name2=val2;name3=val3';
 var nameEQ = name + "=";
 var ca = cookie.split(';');
 var len = ca.length;
 for (var i = 0; i < len; i++) {
  var 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;
})();
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. Here’s a list of current revisions for this page:

0 comments

Add a comment