Skip to content

Commit 6c40f7f

Browse files
ZYSzysBethGriggs
authored andcommitted
querystring: simplify stringify method
PR-URL: #26591 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent db7df0f commit 6c40f7f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

benchmark/querystring/querystring-stringify.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const common = require('../common.js');
33
const querystring = require('querystring');
44

55
const bench = common.createBenchmark(main, {
6-
type: ['noencode', 'encodemany', 'encodelast'],
7-
n: [1e7],
6+
type: ['noencode', 'encodemany', 'encodelast', 'array'],
7+
n: [1e6],
88
});
99

1010
function main({ type, n }) {
@@ -23,6 +23,11 @@ function main({ type, n }) {
2323
foo: 'bar',
2424
baz: 'quux',
2525
xyzzy: 'thu\u00AC'
26+
},
27+
array: {
28+
foo: [],
29+
baz: ['bar'],
30+
xyzzy: ['bar', 'quux', 'thud']
2631
}
2732
};
2833
const input = inputs[type];

lib/querystring.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,19 @@ function stringify(obj, sep, eq, options) {
176176

177177
if (Array.isArray(v)) {
178178
var vlen = v.length;
179+
if (vlen === 0) continue;
179180
var vlast = vlen - 1;
180181
for (var j = 0; j < vlen; ++j) {
181182
fields += ks + encode(stringifyPrimitive(v[j]));
182183
if (j < vlast)
183184
fields += sep;
184185
}
185-
if (vlen && i < flast)
186-
fields += sep;
187186
} else {
188187
fields += ks + encode(stringifyPrimitive(v));
189-
if (i < flast)
190-
fields += sep;
191188
}
189+
190+
if (i < flast)
191+
fields += sep;
192192
}
193193
return fields;
194194
}

0 commit comments

Comments
 (0)