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 (#269)
  • Loading branch information
papandreou authored and ljharb committed Jul 26, 2018
1 parent 47e46a6 commit bc90696
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/stringify.js
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 @@ -85,7 +91,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 @@ -99,7 +105,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
formatter
));
} else {
values = values.concat(stringify(
pushToArray(values, stringify(
obj[key],
prefix + (allowDots ? '.' + key : '[' + key + ']'),
generateArrayPrefix,
Expand Down Expand Up @@ -182,8 +188,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 bc90696

Please sign in to comment.