require 'faker'
require 'dry/core/cache'
require 'dry/core/inflector'
require 'benchmark/ips'
class Context
extend ::Dry::Core::Cache
def get_faker_constant_cache(type)
fetch_or_store(:faker, type) do
::Faker.const_get(::Dry::Core::Inflector.classify(type.to_s))
end
end
def get_faker_constant(type)
::Faker.const_get(::Dry::Core::Inflector.classify(type.to_s))
end
end
context = Context.new
Benchmark.ips do |x|
x.report('cached') do
context.get_faker_constant_cache(:lorem)
end
x.report('non-cached') do
context.get_faker_constant(:lorem)
end
x.compare!
end
Naive benchmark
Results