# How to optimize Cookie Parsing?

I have written this code for cookie parsing. Can anyone tell me what optimizations we can do in this or what can we do to make it more concise?

function parseCookies(cookies) {

return null;
}

// Remove domain from each cookie to decrease the size of cookie (Not needed)
for (var i = 0; i < cookies.length; i++) {

for (var index2 = cookieParts.length - 1; index2 >= 0; index2--) {
key = pair.split('=')[0];

if (key && key.trim().toLowerCase() == 'domain') {
}
}

}

}

• Could you add examples of input and output? As far as I can tell, this isn't so much a parser but rather a sanitizer, since it returns the same format, just with some stuff removed – Flambino Dec 6 '13 at 18:24

Like I wrote in my comment, the function you've got isn't really a parser. Yes, it parses the strings you pass it, but that's not its purpose, as far as I can tell. Parsing is a byproduct of the actual purpose, which seems to be to sanitize the strings by removing the domain key/value pair.

Now, if you're just looking to remove that key/value pair, you could simply do

function sanitizeCookies(strings) {
});
}


Like @tomdemuyt, I'm using Array.map() so you won't get null back if you pass it an empty array; you'll just get another empty array (and, frankly, that makes more sense, I think).

This question is a great candidate for using Array.map and Array.filter. That is, if you are willing to return [] instead null if cookies is not provided.

Basically you map cookies to a new array which has modified cookies, and you filter out the value from the cookie that you don't want.

I did not test this, but something like this ought to do the trick:

function parseCookies(cookies) {