Rescue NameError
or ask if the constant is defined first?
Tested in Ruby 2.3 on OS X
$ bundle
...
$ bundle exec ruby benchmark.rb
Warming up --------------------------------------
undefined const with rescue
98.256k i/100ms
undefined const by asking first
26.705k i/100ms
Calculating -------------------------------------
undefined const with rescue
1.787M (± 2.7%) i/s - 8.941M in 5.008211s
undefined const by asking first
337.107k (± 3.0%) i/s - 1.709M in 5.074571s
Comparison:
undefined const with rescue: 1786652.0 i/s
undefined const by asking first: 337106.6 i/s - 5.30x slower
Warming up --------------------------------------
defined const with rescue
88.743k i/100ms
defined const by asking first
117.461k i/100ms
Calculating -------------------------------------
defined const with rescue
1.442M (± 2.9%) i/s - 7.277M in 5.050590s
defined const by asking first
2.453M (± 3.6%) i/s - 12.333M in 5.034416s
Comparison:
defined const by asking first: 2453319.7 i/s
defined const with rescue: 1442062.0 i/s - 1.70x slower