addyosmani / memoize.js

A faster JavaScript memoizer
http://addyosmani.com
143 stars 19 forks source link

Add try/catch around JSON.stringify? #7

Closed JamieMason closed 7 years ago

JamieMason commented 13 years ago

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.

addyosmani commented 13 years ago

+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.