const customStringify = function (v) { const cache = new Set(); return JSON.stringify(v, function (key, value) { if (typeof value === 'object' && value !== null) { if (cache.has(value)) { // Circular reference found, discard key return; } // Store value in our set cache.add(value); } return value; }); };