Last active
June 26, 2023 19:29
-
-
Save jaynetics/6e19f9cda43dc81a4bf0345a93c0344a to your computer and use it in GitHub Desktop.
String#respond_to? vs ::method_defined?
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
random_strings = (1..100_000).map { ('a'..'z').to_a.shuffle[0, 8].join }; nil | |
Benchmark.bm do |x| | |
x.report { random_strings.each { |s| s.respond_to?(s) } } | |
x.report { random_strings.each { |s| String.method_defined?(s) } } | |
end | |
# => user system total real | |
# => 0.042596 0.001688 0.044284 ( 0.044286) | |
# => 0.011542 0.000012 0.011554 ( 0.011556) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment