Skip to content

Commit

Permalink
[refactor] stringify: Avoid arr = arr.concat(...), push to the exis…
Browse files Browse the repository at this point in the history
…ting instance
  • Loading branch information
papandreou authored and ljharb committed Jul 26, 2018
1 parent 98126ef commit b853cb8
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ var arrayPrefixGenerators = {
}
};

var isArray = Array.isArray;
var push = Array.prototype.push;
var pushToArray = function (arr, valueOrArray) {
push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
};

var toISO = Date.prototype.toISOString;

var defaults = {
Expand Down Expand Up @@ -95,7 +101,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
}

if (Array.isArray(obj)) {
values = values.concat(stringify(
pushToArray(values, stringify(
obj[key],
generateArrayPrefix(prefix, key),
generateArrayPrefix,
Expand All @@ -111,7 +117,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
charset
));
} else {
values = values.concat(stringify(
pushToArray(values, stringify(
obj[key],
prefix + (allowDots ? '.' + key : '[' + key + ']'),
generateArrayPrefix,
Expand Down Expand Up @@ -202,8 +208,7 @@ module.exports = function (object, opts) {
if (skipNulls && obj[key] === null) {
continue;
}

keys = keys.concat(stringify(
pushToArray(keys, stringify(
obj[key],
key,
generateArrayPrefix,
Expand Down

0 comments on commit b853cb8

Please sign in to comment.