Browse Source

Make QueryString.parse() even faster

v0.9.1-release
Brian White 13 years ago
committed by isaacs
parent
commit
5e3ca98155
  1. 24
      lib/querystring.js

24
lib/querystring.js

@ -175,6 +175,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
return obj; return obj;
} }
var regexp = /\+/g;
qs = qs.split(sep); qs = qs.split(sep);
// maxKeys <= 0 means that we should not limit keys count // maxKeys <= 0 means that we should not limit keys count
@ -182,17 +183,18 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
qs = qs.slice(0, maxKeys); qs = qs.slice(0, maxKeys);
} }
qs.forEach(function(kvp) { for (var i = 0, len = qs.length; i < len; ++i) {
var x = kvp.split(eq), k, v, useQS = false; var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
kstr = x.substring(0, idx),
vstr = x.substring(idx + 1), k, v;
try { try {
if (kvp.match(/\+/)) { // decodeURIComponent does not decode + to space k = decodeURIComponent(kstr);
throw 'has +'; v = decodeURIComponent(vstr);
} } catch(e) {
k = decodeURIComponent(x[0]); k = QueryString.unescape(kstr, true);
v = decodeURIComponent(x.slice(1).join(eq) || ''); v = QueryString.unescape(vstr, true);
} catch (e) {
k = QueryString.unescape(x[0], true);
v = QueryString.unescape(x.slice(1).join(eq), true);
} }
if (!hasOwnProperty(obj, k)) { if (!hasOwnProperty(obj, k)) {
@ -202,7 +204,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
} else { } else {
obj[k].push(v); obj[k].push(v);
} }
}); }
return obj; return obj;
}; };

Loading…
Cancel
Save