3 ms·
Why? Genuinely curious.
by hopsoft 11y ago
Why? Genuinely curious.
- chrisseaton 11y ago'try' is relatively slow compared to other control logic, as allowing an exception to be thrown involves constructing an exception stack trace. This is slow in all VMs I know. Theoretically you could detect that the trace isn't used and remove it through escape analysis, but this is not easy, or you could do some crazy thing with lazily creating the stack trace, but that's a research project.
- jrochkind1 11y agoI think you are confused, `try` is not raise/rescue. The ActiveSupport `try` implementation does not involve allowing an exception to be raised. https://github.com/rails/rails/blob/v4.2.5/activesupport/lib/active_support/core_ext/object/try.rb#L69-L80 https://github.com/rails/rails/blob/v4.2.5/activesupport/lib... It should be as performant as other ways of checking for nil/empty.
- chrisseaton 11y agoAh yes you're right - I was confusing this with raise/rescue.
- hopsoft 11y agoI wrote a script to benchmark some of the diff strategies for anyone curious. https://gist.github.com/hopsoft/ae361319c54bbcb4f8e2 https://gist.github.com/hopsoft/ae361319c54bbcb4f8e2
- technion 11y agoNot him, but I try to use patterns that are portable outside Rails where possible.