querystring: simplify stringify method · nodejs/node@6c40f7f (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ const common = require('../common.js');
3 3 const querystring = require('querystring');
4 4
5 5 const bench = common.createBenchmark(main, {
6 -type: ['noencode', 'encodemany', 'encodelast'],
7 -n: [1e7],
6 +type: ['noencode', 'encodemany', 'encodelast', 'array'],
7 +n: [1e6],
8 8 });
9 9
10 10 function main({ type, n }) {
@@ -23,6 +23,11 @@ function main({ type, n }) {
23 23 foo: 'bar',
24 24 baz: 'quux',
25 25 xyzzy: 'thu\u00AC'
26 +},
27 +array: {
28 +foo: [],
29 +baz: ['bar'],
30 +xyzzy: ['bar', 'quux', 'thud']
26 31 }
27 32 };
28 33 const input = inputs[type];
Original file line number Diff line number Diff line change
@@ -176,19 +176,19 @@ function stringify(obj, sep, eq, options) {
176 176
177 177 if (Array.isArray(v)) {
178 178 var vlen = v.length;
179 +if (vlen === 0) continue;
179 180 var vlast = vlen - 1;
180 181 for (var j = 0; j < vlen; ++j) {
181 182 fields += ks + encode(stringifyPrimitive(v[j]));
182 183 if (j < vlast)
183 184 fields += sep;
184 185 }
185 -if (vlen && i < flast)
186 -fields += sep;
187 186 } else {
188 187 fields += ks + encode(stringifyPrimitive(v));
189 -if (i < flast)
190 -fields += sep;
191 188 }
189 +
190 +if (i < flast)
191 +fields += sep;
192 192 }
193 193 return fields;
194 194 }