Skip to content

Instantly share code, notes, and snippets.

@jaynetics
Last active June 26, 2023 19:29
Show Gist options
  • Save jaynetics/6e19f9cda43dc81a4bf0345a93c0344a to your computer and use it in GitHub Desktop.
Save jaynetics/6e19f9cda43dc81a4bf0345a93c0344a to your computer and use it in GitHub Desktop.
String#respond_to? vs ::method_defined?
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