Skip to content

Commit

Permalink
Use match? in FactoryBot#aliases_for to save allocations (#1457)
Browse files Browse the repository at this point in the history
FactoryBot relies on ruby >= 2.5, so we can use Regexp#match? that does not allocate MatchData

Allocations measured with heap-profiler gem in an internal test suite with 7497 examples and 149470 calls to aliases_for:

allocated memory by file
-----------------------------------
    59.79 MB  factory_bot-6.1.0/lib/factory_bot/aliases.rb
    38.27 MB  factory_bot-6.1.0/lib/factory_bot/aliases.rb

allocated objects by file
-----------------------------------
    896880  factory_bot-6.1.0/lib/factory_bot/aliases.rb
    597940  factory_bot-6.1.0/lib/factory_bot/aliases.rb
  • Loading branch information
Aqualon authored Sep 10, 2021
1 parent c80d139 commit 893eb67
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/factory_bot/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self

def self.aliases_for(attribute)
aliases.map { |(pattern, replace)|
if pattern.match(attribute.to_s)
if pattern.match?(attribute)
attribute.to_s.sub(pattern, replace).to_sym
end
}.compact << attribute
Expand Down

0 comments on commit 893eb67

Please sign in to comment.