From 6c40f7f940fc07c9a65801af2e897493c428360a Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Wed, 27 Mar 2019 11:21:49 +0800 Subject: [PATCH] querystring: simplify stringify method PR-URL: https://github.com/nodejs/node/pull/26591 Reviewed-By: Ruben Bridgewater Signed-off-by: Beth Griggs --- benchmark/querystring/querystring-stringify.js | 9 +++++++-- lib/querystring.js | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/benchmark/querystring/querystring-stringify.js b/benchmark/querystring/querystring-stringify.js index 9f025c922ad4e8..6c5da0f464034f 100644 --- a/benchmark/querystring/querystring-stringify.js +++ b/benchmark/querystring/querystring-stringify.js @@ -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 }) { @@ -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]; diff --git a/lib/querystring.js b/lib/querystring.js index 240e2e70d6a3f8..8d97e288c12271 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -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; }