As @DmitryBaranovskiy commented;
"Should we add try/catch around JSON.stringify? There is a case when somebody passes cyclic structure to the function. It could not be able to cache it, but at least it should return the value."
var a = { b: null },
b = { a: a };
a.b = b;
JSON.stringify(a); // >> TypeError: circular_structure
Trade-off is working code in 100% of cases vs performant code for majority of cases, with some broken edge cases.
+1 for code working in 100% of cases. We can always benchmark after, but having a solid implementation that others can use in almost any common situation is worth the effort.
As @DmitryBaranovskiy commented; "Should we add try/catch around JSON.stringify? There is a case when somebody passes cyclic structure to the function. It could not be able to cache it, but at least it should return the value."
Trade-off is working code in 100% of cases vs performant code for majority of cases, with some broken edge cases.