How slow are Ruby exceptions as compared to other frequent actions we may be doing in Rails apps. Like for example, as compared to ActiveRecord::Base#create

Struggling with finding Senior Ruby developers? - Show your job post here and reach thousands of developers quickly.

Big thanks to Robert Pankowecki who created the original gist and to Piotr Szotkowski who provided even more data. The gist was originally created as part of our Fearless Refactoring: Rails Controllers book where Ruby exceptions are suggested as one of the possible techniques of the controller communication with service objects.

require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.logger = nil
ActiveRecord::Base.establish_connection adapter:  'postgresql',
                                        database: 'whatevers'

Whatever =

Benchmark.ips do |bench|'SQL query')      { Whatever.create(text: 'meh') }'exception hit')  { raise rescue nil }'exception miss') { raise if false }!

Then we can run it with:

ruby -v bench.rb

with results like this:

ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
Warming up --------------------------------------
           SQL query    18.000  i/100ms
       exception hit    57.640k i/100ms
      exception miss   269.546k i/100ms
Calculating -------------------------------------
           SQL query    182.124  (±15.9%) i/s -    882.000  in   5.003193s
       exception hit    808.613k (± 4.8%) i/s -      4.035M in   5.004163s
      exception miss     10.129M (± 3.2%) i/s -     50.675M in   5.007747s

      exception miss: 10129279.5 i/s
       exception hit:   808613.1 i/s - 12.53x slower
           SQL query:      182.1 i/s - 55617.43x slower

which means that for this configuration, the exceptions are 4438 times faster than AR::Base#create.