class RuboCop::Cop::SketchupPerformance::TypeCheck

String comparisons for type checks are very slow.

`entity.class.name == 'Sketchup::Face'` is slow because it performs a string comparison. `is_a?` is much faster because it's a simple type check.

If you know you need a strict type check, compare the classes directly: `entity.class == Sketchup::Face`.

@example Poor performance

entity.class.name == 'Sketchup::Face'

@example Good performance

entity.class == Sketchup::Face

@example Good performance and idiomatic Ruby convention

entity.is_a?(Sketchup::Face)

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/sketchup/cop/performance/type_check.rb, line 44
def on_send(node)
  return unless string_class_compare?(node)

  add_offense(node, location: comparison_range(node))
end

Private Instance Methods

comparison_range(node) click to toggle source
# File lib/rubocop/sketchup/cop/performance/type_check.rb, line 52
def comparison_range(node)
  lhs = node.receiver
  rhs = node.arguments.first

  loc_begin = lhs.receiver.loc.selector.begin_pos
  loc_end = rhs.loc.expression.end_pos
  range_between(loc_begin, loc_end)
end