findCycle performance
I found myself with my relatively large app, which suddenly became slow on mounting a complex component.
Profiling shown that findCycle function was running for ~3 seconds, while the rest was about 100ms. Apparently it's complexity is exponential in full DAG for example. Wrote a quick comparision of current version of this function and quick alternative with memoizing result: https://jsfiddle.net/s5hsp8ju/
It probably still can be fine tuned, but already runs 500 000 times faster for 30 nodes :)
Quick update of test case: https://jsfiddle.net/s5hsp8ju/1/
Just switched
_findCycle(nodes[MAX - 2], nodes[MAX - 1]) to
_findCycle(nodes[MAX - 1], nodes[MAX - 2]). first one could work with first approach if we itarated list in reversed order, second one cannot be solved significantly faster with the first approach.
Thanks for the PoC! I will play with it and either use the Map setup (with some fallback for other browsers) or see whether it is possible to make the findCycle an after-the-fact (e.g. stack overflow) analysis or a debug only thing so that it doesn't incur any runtime overhead at all
Now, that I run mobx with my quickfix, findCycle is unnoticeable in timelime, so no pressure to get rid of it in production. It's also nice to get an elegant error, instead of overflowing stack.
Turned out findCycle didn't caught any cycles anymore which were not already caught (at a later stage) by the internal invariants. So just removed the entire thing :). Nothing beats that. Thanks for looking into this and suggesting the improvements though! Released as 2.3.4