Parse cookies

JavaScript performance comparison

Revision 4 of this test case created by Simon

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    encode = encodeURIComponent;
   
    decode = decodeURIComponent;
   
    function parse(str) {
      var obj = {};
      var pairs = str.split(/ *; */);
      var pair;
      if ('' == pairs[0]) return obj;
      for (var i = 0; i < pairs.length; ++i) {
        pair = pairs[i].split('=');
        obj[decode(pair[0])] = decode(pair[1]);
      }
      return obj;
    }
   
    getAllCookies = function(str) {
      var colIx, egalIx, obj, pair, prevIx;
      obj = {};
      prevIx = 0;
      //str = decode(str);
      while (true) {
        colIx = str.indexOf(';', prevIx);
        pair = colIx === -1 ? str.substring(prevIx).trim() : str.substring(prevIx, colIx).trim();
        if (pair === '') {
          return obj;
        }
        egalIx = pair.indexOf('=');
        obj[pair.substring(0, egalIx)] = pair.substring(egalIx + 1);
        if (colIx === -1) {
          return obj;
        }
        prevIx = colIx + 1;
      }
    };
   
    var testCoo = '__qca=P0-1279630214-1361530382272; gauthed=1; sgt=id=c0de28d9-13bc-4c2d-bd31-377f58a5ef52';
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
express parse
parse(testCoo);
pending…
getAllCookies
getAllCookies(testCoo);
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