4 ms·
You can use Douglas Crockford cycle tools in order to deal with this. https://github.com/douglascrockford/JSON-js/blob/master/cycle.js https://github.com/dougl
by vjeux 15y ago
You can use Douglas Crockford cycle tools in order to deal with this.
https://github.com/douglascrockford/JSON-js/blob/master/cycle.js 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.
- josscrowcroft 15y agoThanks 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?
- vjeux 15y agohttp://jsperf.com/object-cycle-detection 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.