Created
March 13, 2023 23:06
-
-
Save taq/6b6c5ba3c6748f4f8f0a863367e649a5 to your computer and use it in GitHub Desktop.
Comparação com o ActiveSupport::StringInquirer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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