summaryrefslogtreecommitdiff
path: root/lib/querystring.js
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2019-08-25 00:27:53 -0400
committerBrian White <mscdex@mscdex.net>2019-08-28 03:12:11 -0400
commit248e9ec1247af05f0b21885814c89165c281a64e (patch)
treece55cca000ae75dc7ecd23e11dbbb2a0b005189b /lib/querystring.js
parent3ae6f5e1169f423e71036d1bbd62f2c26b136914 (diff)
downloadnode-new-248e9ec1247af05f0b21885814c89165c281a64e.tar.gz
querystring: improve performance
PR-URL: https://github.com/nodejs/node/pull/29306 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Diffstat (limited to 'lib/querystring.js')
-rw-r--r--lib/querystring.js68
1 files changed, 31 insertions, 37 deletions
diff --git a/lib/querystring.js b/lib/querystring.js
index a09dc7d426..9fac6e2627 100644
--- a/lib/querystring.js
+++ b/lib/querystring.js
@@ -174,19 +174,22 @@ function stringify(obj, sep, eq, options) {
for (var i = 0; i < len; ++i) {
var k = keys[i];
var v = obj[k];
- var ks = encode(stringifyPrimitive(k)) + eq;
+ var ks = encode(stringifyPrimitive(k));
+ ks += eq;
if (Array.isArray(v)) {
var vlen = v.length;
if (vlen === 0) continue;
var vlast = vlen - 1;
for (var j = 0; j < vlen; ++j) {
- fields += ks + encode(stringifyPrimitive(v[j]));
+ fields += ks;
+ fields += encode(stringifyPrimitive(v[j]));
if (j < vlast)
fields += sep;
}
} else {
- fields += ks + encode(stringifyPrimitive(v));
+ fields += ks;
+ fields += encode(stringifyPrimitive(v));
}
if (i < flast)
@@ -200,14 +203,34 @@ function stringify(obj, sep, eq, options) {
function charCodes(str) {
if (str.length === 0) return [];
if (str.length === 1) return [str.charCodeAt(0)];
- const ret = [];
+ const ret = new Array(str.length);
for (var i = 0; i < str.length; ++i)
- ret[ret.length] = str.charCodeAt(i);
+ ret[i] = str.charCodeAt(i);
return ret;
}
const defSepCodes = [38]; // &
const defEqCodes = [61]; // =
+function addKeyVal(obj, key, value, keyEncoded, valEncoded, decode) {
+ if (key.length > 0 && keyEncoded)
+ key = decodeStr(key, decode);
+ if (value.length > 0 && valEncoded)
+ value = decodeStr(value, decode);
+
+ if (obj[key] === undefined) {
+ obj[key] = value;
+ } else {
+ const curValue = obj[key];
+ // A simple Array-specific property check is enough here to
+ // distinguish from a string value and is faster and still safe
+ // since we are generating all of the values being assigned.
+ if (curValue.pop)
+ curValue[curValue.length] = value;
+ else
+ obj[key] = [curValue, value];
+ }
+}
+
// Parse a key/val string.
function parse(qs, sep, eq, options) {
const obj = Object.create(null);
@@ -272,23 +295,8 @@ function parse(qs, sep, eq, options) {
value += qs.slice(lastPos, end);
}
- if (key.length > 0 && keyEncoded)
- key = decodeStr(key, decode);
- if (value.length > 0 && valEncoded)
- value = decodeStr(value, decode);
+ addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);
- if (obj[key] === undefined) {
- obj[key] = value;
- } else {
- const curValue = obj[key];
- // A simple Array-specific property check is enough here to
- // distinguish from a string value and is faster and still safe
- // since we are generating all of the values being assigned.
- if (curValue.pop)
- curValue[curValue.length] = value;
- else
- obj[key] = [curValue, value];
- }
if (--pairs === 0)
return obj;
keyEncoded = valEncoded = customDecode;
@@ -370,22 +378,8 @@ function parse(qs, sep, eq, options) {
// We ended on an empty substring
return obj;
}
- if (key.length > 0 && keyEncoded)
- key = decodeStr(key, decode);
- if (value.length > 0 && valEncoded)
- value = decodeStr(value, decode);
- if (obj[key] === undefined) {
- obj[key] = value;
- } else {
- const curValue = obj[key];
- // A simple Array-specific property check is enough here to
- // distinguish from a string value and is faster and still safe since
- // we are generating all of the values being assigned.
- if (curValue.pop)
- curValue[curValue.length] = value;
- else
- obj[key] = [curValue, value];
- }
+
+ addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);
return obj;
}