Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Make QueryString.parse() even faster
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex authored and isaacs committed Feb 21, 2012
1 parent 3817b12 commit 5e3ca98
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/querystring.js
Expand Up @@ -175,24 +175,26 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
return obj;
}

var regexp = /\+/g;
qs = qs.split(sep);

// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0) {
qs = qs.slice(0, maxKeys);
}

qs.forEach(function(kvp) {
var x = kvp.split(eq), k, v, useQS = false;
for (var i = 0, len = qs.length; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
kstr = x.substring(0, idx),
vstr = x.substring(idx + 1), k, v;

try {
if (kvp.match(/\+/)) { // decodeURIComponent does not decode + to space
throw 'has +';
}
k = decodeURIComponent(x[0]);
v = decodeURIComponent(x.slice(1).join(eq) || '');
} catch (e) {
k = QueryString.unescape(x[0], true);
v = QueryString.unescape(x.slice(1).join(eq), true);
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
} catch(e) {
k = QueryString.unescape(kstr, true);
v = QueryString.unescape(vstr, true);
}

if (!hasOwnProperty(obj, k)) {
Expand All @@ -202,7 +204,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
} else {
obj[k].push(v);
}
});
}

return obj;
};

0 comments on commit 5e3ca98

Please sign in to comment.