Skip to content

Commit d390c99

Browse files
committedDec 16, 2024
[Robustness] cache more builtins
1 parent 850b24c commit d390c99

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed
 

‎index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ var callBind = require('call-bind');
88
var callBound = require('call-bound');
99

1010
var $join = callBound('Array.prototype.join');
11-
var $push = callBound('Array.prototype.push');
11+
var $indexOf = callBound('Array.prototype.indexOf');
12+
var $splice = callBound('Array.prototype.splice');
13+
var $sort = callBound('Array.prototype.sort');
1214

1315
var strRepeat = function repeat(n, char) {
1416
var str = '';
@@ -60,17 +62,19 @@ module.exports = function stableStringify(obj) {
6062
var out = [];
6163
for (var i = 0; i < node.length; i++) {
6264
var item = stringify(node, i, node[i], level + 1) || jsonStringify(null);
63-
$push(out, indent + space + item);
65+
out[out.length] = indent + space + item;
6466
}
6567
return '[' + $join(out, ',') + indent + ']';
6668
}
6769

68-
if (seen.indexOf(node) !== -1) {
70+
if ($indexOf(seen, node) !== -1) {
6971
if (cycles) { return jsonStringify('__cycle__'); }
7072
throw new TypeError('Converting circular structure to JSON');
71-
} else { $push(seen, node); }
73+
} else {
74+
seen[seen.length] = node;
75+
}
7276

73-
var keys = objectKeys(node).sort(cmp && cmp(node));
77+
var keys = $sort(objectKeys(node), cmp && cmp(node));
7478
var out = [];
7579
for (var i = 0; i < keys.length; i++) {
7680
var key = keys[i];
@@ -82,9 +86,9 @@ module.exports = function stableStringify(obj) {
8286
+ colonSeparator
8387
+ value;
8488

85-
$push(out, indent + space + keyValue);
89+
out[out.length] = indent + space + keyValue;
8690
}
87-
seen.splice(seen.indexOf(node), 1);
91+
$splice(seen, $indexOf(seen, node), 1);
8892
return '{' + $join(out, ',') + indent + '}';
8993

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

0 commit comments

Comments
 (0)
Please sign in to comment.