Skip to content

Instantly share code, notes, and snippets.

@taq
Created March 13, 2023 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taq/6b6c5ba3c6748f4f8f0a863367e649a5 to your computer and use it in GitHub Desktop.
Save taq/6b6c5ba3c6748f4f8f0a863367e649a5 to your computer and use it in GitHub Desktop.
Comparação com o ActiveSupport::StringInquirer
require 'active_support/string_inquirer'
require 'benchmark'
# classe
class ClientExternalData
# o método com o inquirer/method_missing
def identifier
ActiveSupport::StringInquirer.new('uuid')
end
# o método comparando com símbolo
def s_identifier?(id)
id == :uuid
end
end
data = ClientExternalData.new
count = 100_000
# aqui nós conferimos o resultado de um ...
puts data.identifier.uuid?
puts data.identifier.id?
# e depois do outro.
puts data.s_identifier?(:uuid)
puts data.s_identifier?(:id)
# e disparamos os benchmarks!
Benchmark.bm do |bm|
bm.report 'com StringInquirer' do
count.times do
data.identifier.uuid?
data.identifier.id?
end
end
bm.report 'usando com regex' do
count.times do
data.s_identifier?(:uuid)
data.s_identifier?(:id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment