Skip to content
Permalink
Browse files

querystring: improve performance

PR-URL: #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>
  • Loading branch information...
mscdex authored and BridgeAR committed Aug 25, 2019
1 parent bd1e8ea commit 0e1ccca81dc9db439865058b67bdd7d82a92d1ed
Showing with 31 additions and 37 deletions.
  1. +31 −37 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;
}

0 comments on commit 0e1ccca

Please sign in to comment.
You can’t perform that action at this time.