Skip to content

Commit

Permalink
[Robustness] cache JSON.stringify at module load
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 27, 2022
1 parent 1226971 commit 616dec3
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
var jsonStringify = (typeof JSON !== 'undefined' ? JSON : require('jsonify')).stringify;

var isArray = Array.isArray || function (x) {
return {}.toString.call(x) === '[object Array]';
Expand Down Expand Up @@ -48,19 +48,19 @@ module.exports = function (obj, opts) {
return;
}
if (typeof node !== 'object' || node === null) {
return json.stringify(node);
return jsonStringify(node);
}
if (isArray(node)) {
var out = [];
for (var i = 0; i < node.length; i++) {
var item = stringify(node, i, node[i], level + 1) || json.stringify(null);
var item = stringify(node, i, node[i], level + 1) || jsonStringify(null);
out.push(indent + space + item);
}
return '[' + out.join(',') + indent + ']';
}

if (seen.indexOf(node) !== -1) {
if (cycles) { return json.stringify('__cycle__'); }
if (cycles) { return jsonStringify('__cycle__'); }
throw new TypeError('Converting circular structure to JSON');
} else { seen.push(node); }

Expand All @@ -72,7 +72,7 @@ module.exports = function (obj, opts) {

if (!value) { continue; }

var keyValue = json.stringify(key)
var keyValue = jsonStringify(key)
+ colonSeparator
+ value;

Expand Down

0 comments on commit 616dec3

Please sign in to comment.