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 0669d58 commit 9b96ace
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 @@ -88,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 @@ -103,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 @@ -188,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 9b96ace

Please sign in to comment.