Skip to content

Commit

Permalink
querystring: simplify stringify method
Browse files Browse the repository at this point in the history
PR-URL: #26591
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
  • Loading branch information
ZYSzys authored and BethGriggs committed Apr 10, 2019
1 parent ddf7477 commit 038b778
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions benchmark/querystring/querystring-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const common = require('../common.js');
const querystring = require('querystring');

const bench = common.createBenchmark(main, {
type: ['noencode', 'encodemany', 'encodelast'],
n: [1e7],
type: ['noencode', 'encodemany', 'encodelast', 'array'],
n: [1e6],
});

function main({ type, n }) {
Expand All @@ -23,6 +23,11 @@ function main({ type, n }) {
foo: 'bar',
baz: 'quux',
xyzzy: 'thu\u00AC'
},
array: {
foo: [],
baz: ['bar'],
xyzzy: ['bar', 'quux', 'thud']
}
};
const input = inputs[type];
Expand Down
8 changes: 4 additions & 4 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ function stringify(obj, sep, eq, options) {

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]));
if (j < vlast)
fields += sep;
}
if (vlen && i < flast)
fields += sep;
} else {
fields += ks + encode(stringifyPrimitive(v));
if (i < flast)
fields += sep;
}

if (i < flast)
fields += sep;
}
return fields;
}
Expand Down

0 comments on commit 038b778

Please sign in to comment.