querystring: simplify stringify method
PR-URL: https://github.com/nodejs/node/pull/26591 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
eb2dccb17a
commit
b965ac22ca
@ -3,8 +3,8 @@ const common = require('../common.js');
|
|||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
|
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
type: ['noencode', 'encodemany', 'encodelast'],
|
type: ['noencode', 'encodemany', 'encodelast', 'array'],
|
||||||
n: [1e7],
|
n: [1e6],
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ type, n }) {
|
function main({ type, n }) {
|
||||||
@ -23,6 +23,11 @@ function main({ type, n }) {
|
|||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
baz: 'quux',
|
baz: 'quux',
|
||||||
xyzzy: 'thu\u00AC'
|
xyzzy: 'thu\u00AC'
|
||||||
|
},
|
||||||
|
array: {
|
||||||
|
foo: [],
|
||||||
|
baz: ['bar'],
|
||||||
|
xyzzy: ['bar', 'quux', 'thud']
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const input = inputs[type];
|
const input = inputs[type];
|
||||||
|
@ -176,20 +176,20 @@ function stringify(obj, sep, eq, options) {
|
|||||||
|
|
||||||
if (Array.isArray(v)) {
|
if (Array.isArray(v)) {
|
||||||
var vlen = v.length;
|
var vlen = v.length;
|
||||||
|
if (vlen === 0) continue;
|
||||||
var vlast = vlen - 1;
|
var vlast = vlen - 1;
|
||||||
for (var j = 0; j < vlen; ++j) {
|
for (var j = 0; j < vlen; ++j) {
|
||||||
fields += ks + encode(stringifyPrimitive(v[j]));
|
fields += ks + encode(stringifyPrimitive(v[j]));
|
||||||
if (j < vlast)
|
if (j < vlast)
|
||||||
fields += sep;
|
fields += sep;
|
||||||
}
|
}
|
||||||
if (vlen && i < flast)
|
|
||||||
fields += sep;
|
|
||||||
} else {
|
} else {
|
||||||
fields += ks + encode(stringifyPrimitive(v));
|
fields += ks + encode(stringifyPrimitive(v));
|
||||||
|
}
|
||||||
|
|
||||||
if (i < flast)
|
if (i < flast)
|
||||||
fields += sep;
|
fields += sep;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user