Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One more point - `JSON.stringify` returns an error when attempting to stringify a circular structure such as the `window` object...

I tried `URLON.stringify(window)` and nothing happened, no error or return value (bad sign) so hit it a few more times, saw a couple stack overflow errors, and the tab crashed.

If possible it should definitely throw a RangeError instead of attempting to stringify a circular structure.



You can use Douglas Crockford cycle tools in order to deal with this.

https://github.com/douglascrockford/JSON-js/blob/master/cycl...

As the only way to know if an object has already been visited is in linear time in Javascript, it makes cycle detection a O(n^2) process. This is way too costly to add by default unfortunately.


Thanks for the link, I see what you mean about costly.

Perhaps a way to hack-fix this is to set a parameter for maxRecursion, to prevent the errors - seems like a pretty solid way of preventing endless loops (RangeErrors) but is probably something that should be off-by-default and switched on globally via a setting or once via a parameter...

I can add it and submit a pull-request for your consideration if you like?


http://jsperf.com/object-cycle-detection

I've added a jsPerf to see how bad it performs. For small objects it's reasonable but if the object has 10 000 elements the Javascript test will take 1 second, which is not acceptable.

I'd rather give people a detection cycle detection library if their input may be circular.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: