fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

String#constantize vs simple comparison #200

Closed wasaylor closed 2 years ago

wasaylor commented 2 years ago

If you're writing a Rails app, you may reach for String#constantize (provided by Active Support Core Extensions) when you have a variable that may resolve to a class name and your control flow is to execute that classes code in some manner.

This benchmarks using String#constantize vs a simple comparison, showing the latter is much faster.