Cookie Parsing (v7)

Revision 7 of this benchmark created on


Description

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

Ready to run.

Testing in
TestOps/sec
String
(function(name, cookie) {

        var name = 'name2',
        cookie = 'name1=val1;name2=val2;name3=val3',
        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];

})('name2', 'name1=val1;name2=val2;name3=val3');
ready
RegExp
(function(name, cookie) {

         var regex = new RegExp(name + '=([^;]*)', 'g'),
             result = regex.exec(cookie);

         return result[1] || null;

})('name2', 'name1=val1;name2=val2;name3=val3');
ready
Lawnchair
(function(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;

})('name2', 'name1=val1;name2=val2;name3=val3');
ready
Simple splits
(function(name, cookie) {

        var parts = cookie.split(name);

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

})('name2', 'name1=val1;name2=val2;name3=val3');
ready

Revisions

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