Skip to content

Instantly share code, notes, and snippets.

@vojtad
Last active December 6, 2018 20:35
Show Gist options
  • Save vojtad/5f39a0fcecd40f83c712ecb368d81553 to your computer and use it in GitHub Desktop.
Save vojtad/5f39a0fcecd40f83c712ecb368d81553 to your computer and use it in GitHub Desktop.
Benchmark: two ways of checking if module exists

Benchmarking two ways of checking if module is defined using its name in string.

begin
  'ModuleName'.constantize
rescue NameError
  # ignored
end
Object.const_defined?('ModuleName')

Runtime

Warming up --------------------------------------
module exists with rescuing NameError
                        70.687k i/100ms
module does not exist with rescuing NameError
                        49.000  i/100ms
module exists with const_defined?
                       164.487k i/100ms
module does not exist with const_defined?
                       113.492k i/100ms
Calculating -------------------------------------
module exists with rescuing NameError
                        775.299k (±17.3%) i/s -      3.746M in   5.060236s
module does not exist with rescuing NameError
                        412.667  (±14.5%) i/s -      2.009k in   5.004577s
module exists with const_defined?
                          2.470M (±17.0%) i/s -     12.008M in   5.056179s
module does not exist with const_defined?
                          1.540M (± 5.0%) i/s -      7.717M in   5.024414s

Comparison:
module exists with const_defined?:  2469636.9 i/s
module does not exist with const_defined?:  1539832.8 i/s - 1.60x  slower
module exists with rescuing NameError:   775299.4 i/s - 3.19x  slower
module does not exist with rescuing NameError:      412.7 i/s - 5984.57x  slower

Memory

Calculating -------------------------------------
module exists with rescuing NameError
                       120.000  memsize (     0.000  retained)
                         3.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
module does not exist with rescuing NameError
                       112.709k memsize (     0.000  retained)
                         1.339k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)
module exists with const_defined?
                        40.000  memsize (     0.000  retained)
                         1.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
module does not exist with const_defined?
                        80.000  memsize (     0.000  retained)
                         2.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Comparison:
module exists with const_defined?:         40 allocated
module does not exist with const_defined?:         80 allocated - 2.00x more
module exists with rescuing NameError:        120 allocated - 3.00x more
module does not exist with rescuing NameError:     112709 allocated - 2817.72x more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment