-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular references throw stack overflows #16
Comments
Is it undesirable or impossible for immutable data structures to handle cycles? |
A better question is: why do you think it is unwise to support circular references? I am merely curious, of course |
Just that it's time-consuming both from an implementation and from a performance perspective. It would entail building up an intermediate data structure just to track where things need to end up, then converting that back into a cloned object with references appropriately reconnected... This came up because I accidentally called |
That said, if it's something someone actually needs, I'm not aware of any reason it couldn't be done. |
Yep, makes total sense. seems smart to me |
Now that there is a dev/prod mode maybe it would make sense to check this only in development mode? That way most cases can be found without sacrificing any performance. A really simple way would be to do a test before making it immutable, like: If it contains a circular reference it will throw a |
Great point! It could also then |
FYI I was creating an immutable copy of a Backbone object that was using the backbone-associations add-on and ran into this. immutablejs handles them fine but I liked the sound of this library for all the reasons mentioned in your blog: http://noredinktech.tumblr.com/post/107617838018/switching-from-immutable-js-to-seamless-immutable |
Resolved by #119 |
If you call
Immutable(obj)
on anobj
that contains a circular reference, you get a stack overflow as it tries to clone it.I don't think supporting objects containing circular references is a good idea, but you should at least get a nice error message.
A heuristic could be useful here for performance, e.g. start by just keep count of how big the stack is getting; if it gets over (let's say) 1,000 frames, start over with circular reference detection.
The text was updated successfully, but these errors were encountered: