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

Commit 5e3ca98

Browse files
mscdexisaacs
authored andcommitted
Make QueryString.parse() even faster
1 parent 3817b12 commit 5e3ca98

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

lib/querystring.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,26 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
175175
return obj;
176176
}
177177

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

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

185-
qs.forEach(function(kvp) {
186-
var x = kvp.split(eq), k, v, useQS = false;
186+
for (var i = 0, len = qs.length; i < len; ++i) {
187+
var x = qs[i].replace(regexp, '%20'),
188+
idx = x.indexOf(eq),
189+
kstr = x.substring(0, idx),
190+
vstr = x.substring(idx + 1), k, v;
191+
187192
try {
188-
if (kvp.match(/\+/)) { // decodeURIComponent does not decode + to space
189-
throw 'has +';
190-
}
191-
k = decodeURIComponent(x[0]);
192-
v = decodeURIComponent(x.slice(1).join(eq) || '');
193-
} catch (e) {
194-
k = QueryString.unescape(x[0], true);
195-
v = QueryString.unescape(x.slice(1).join(eq), true);
193+
k = decodeURIComponent(kstr);
194+
v = decodeURIComponent(vstr);
195+
} catch(e) {
196+
k = QueryString.unescape(kstr, true);
197+
v = QueryString.unescape(vstr, true);
196198
}
197199

198200
if (!hasOwnProperty(obj, k)) {
@@ -202,7 +204,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
202204
} else {
203205
obj[k].push(v);
204206
}
205-
});
207+
}
206208

207209
return obj;
208210
};

0 commit comments

Comments
 (0)