Skip to content

Commit

Permalink
Avoid arr = arr.concat(...), push to the existing instance
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 26, 2018
1 parent 2b94ea7 commit 4df4608
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ var arrayPrefixGenerators = {
}
};

var pushToArray = function (arr, valueOrArray) {
if (Array.isArray(valueOrArray)) {
Array.prototype.push.apply(arr, valueOrArray);
} else {
arr.push(valueOrArray);
}
};

var toISO = Date.prototype.toISOString;

var defaults = {
Expand Down Expand Up @@ -86,7 +94,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 @@ -101,7 +109,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
encodeValuesOnly
));
} else {
values = values.concat(stringify(
pushToArray(values, stringify(
obj[key],
prefix + (allowDots ? '.' + key : '[' + key + ']'),
generateArrayPrefix,
Expand Down Expand Up @@ -186,8 +194,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 4df4608

Please sign in to comment.