Skip to content

Commit

Permalink
set default parameter to nil to speed up attribute_changed?
Browse files Browse the repository at this point in the history
Benchmark results:

Warming up --------------------------------------
            old code    32.176k i/100ms
            new code    34.837k i/100ms
Calculating -------------------------------------
            old code      1.595M (± 3.5%) i/s -      7.947M
            new code      1.942M (± 3.9%) i/s -      9.685M
  • Loading branch information
lihanli committed Apr 11, 2016
1 parent fa86026 commit 73e2dbe
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions activemodel/lib/active_model/dirty.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ def changed_attributes
end end


# Handles <tt>*_changed?</tt> for +method_missing+. # Handles <tt>*_changed?</tt> for +method_missing+.
def attribute_changed?(attr, options = {}) #:nodoc: def attribute_changed?(attr, options = nil) #:nodoc:
result = changes_include?(attr) result = changes_include?(attr)
result &&= options[:to] == __send__(attr) if options.key?(:to) if options
result &&= options[:from] == changed_attributes[attr] if options.key?(:from) result &&= options[:to] == __send__(attr) if options.key?(:to)
result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
end
result result
end end


Expand Down

0 comments on commit 73e2dbe

Please sign in to comment.