Skip to content

Conversation

gkampjes
Copy link
Contributor

Description of the change

Calling user.send(nil) raises a TypeError, which is subsequently swallowed. This is much less performant than using a conditional to guard against calling user.send(nil) (see benchmark at bottom of PR).

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Maintenance
  • New release

Checklists

Development

  • Lint rules pass locally
    • Metrics/CyclomaticComplexity [10/7]
    • Metrics/MethodLength [23/15]
    • Metrics/PerceivedComplexity [10/8]
    • I can break it down into a couple of smaller methods if needed
  • The code changed/added as part of this pull request has been covered with tests
    • Already covered by existing tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers assigned
  • Issue from task tracker has a link to this pull request
  • Changes have been reviewed by at least one other engineer

object = Object.new
method_name = nil

Benchmark.ips do |x|
  x.report('without guard') do
    object.send(method_name)
  rescue StandardError
    nil
  end

  x.report('guarded') do
    object.send(method_name) if method_name
  rescue StandardError
    nil
  end

  x.compare!
end

# Warming up --------------------------------------
#        without guard    70.391k i/100ms
#              guarded     2.001M i/100ms
# Calculating -------------------------------------
#        without guard    694.934k (± 2.8%) i/s -      3.520M in   5.068625s
#              guarded     20.060M (± 3.2%) i/s -    102.066M in   5.093412s
# 
# Comparison:
#              guarded: 20059621.4 i/s
#        without guard:   694934.2 i/s - 28.87x  slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant