Skip to content

Commit

Permalink
[Refactor] build up a string instead of an array + join
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 11, 2023
1 parent 4e95ebb commit 6c066b8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ module.exports = function (obj, opts) {
return jsonStringify(node);
}
if (isArray(node)) {
var out = [];
var out = '';
for (var i = 0; i < node.length; i++) {
var item = stringify(node, i, node[i], level + 1) || jsonStringify(null);
out.push(indent + space + item);
out += indent + space + item;
if ((i + 1) < node.length) {
out += ',';
}
}
return '[' + out.join(',') + indent + ']';
return '[' + out + indent + ']';
}

if (seen.indexOf(node) !== -1) {
Expand All @@ -68,7 +71,8 @@ module.exports = function (obj, opts) {
} else { seen.push(node); }

var keys = objectKeys(node).sort(cmp && cmp(node));
var out = [];
var out = '';
var needsComma = false;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node, key, node[key], level + 1);
Expand All @@ -79,10 +83,11 @@ module.exports = function (obj, opts) {
+ colonSeparator
+ value;

out.push(indent + space + keyValue);
out += (needsComma ? ',' : '') + indent + space + keyValue;
needsComma = true;
}
seen.splice(seen.indexOf(node), 1);
return '{' + out.join(',') + indent + '}';
return '{' + out + indent + '}';

}({ '': obj }, '', obj, 0));
};

0 comments on commit 6c066b8

Please sign in to comment.