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

Make QueryString.parse() even faster #2668

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/querystring.js
Expand Up @@ -169,17 +169,18 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) {
return obj;
}

qs.split(sep).forEach(function(kvp) {
var x = kvp.split(eq), k, v, useQS=false;
var qssplit = qs.split(sep), regexp = /\+/g;
for (var i = 0, len = qssplit.length; i < len; ++i) {
var x = qssplit[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) || "");
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
} catch(e) {
k = QueryString.unescape(x[0], true);
v = QueryString.unescape(x.slice(1).join(eq), true);
k = QueryString.unescape(kstr, true);
v = QueryString.unescape(vstr, true);
}

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

return obj;
};