Hacker News new | ask | show | jobs
by hopsoft 3831 days ago
Why? Genuinely curious.
2 comments

'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.
I wrote a script to benchmark some of the diff strategies for anyone curious. https://gist.github.com/hopsoft/ae361319c54bbcb4f8e2
I 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...

It should be as performant as other ways of checking for nil/empty.

Ah yes you're right - I was confusing this with raise/rescue.
Not him, but I try to use patterns that are portable outside Rails where possible.