This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Make QueryString.parse() even faster

  • Loading branch information...
mscdex authored and isaacs committed Feb 21, 2012
1 parent 3817b12 commit 5e3ca981556c305830553d006b76d5dcaaf49276
Showing with 13 additions and 11 deletions.
  1. +13 −11 lib/querystring.js
View
@@ -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)) {
@@ -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.