Open kownacki opened 8 years ago
Preventing against this in all cases involves maintaining an array (or a Set in newer browsers, but we'd have to have the array fallback) of all seen objects. Currently, we support one-level recursion, but your example here is 3-4 levels. Although your example's cycle includes the root object, you can easily see how you'd construct an example where the cycle did not include the root object.
I'll leave this open because I'm interested in solving the problem, but I don't want to prohibitively slow down the common use case to support an edge case, if I can avoid it.
You can avoid slowing down if you provide a flag indicating if given object may contain cycles longer than one. For example isEqual(a, b, true)
will mean that a and b may contain cycles and a slower and heavier algorithm should be used. Your choice is is which case will be default.
That's not a bad idea - enabling deep cycle detection via a flag. I'll think on that.
It's a common pattern in many utility libraries. For example http://underscorejs.org/#uniq you can say that your array is sorted which will allow faster algorithm.
I've played with this a bit; I'm not actually sure how to implement this in a generic fashion. a PR is welcome.
I get
RangeError: Maximum call stack size exceeded