Pattern: Extra array allocation
Issue: -
compact
, flatten
or map
will generate a new intermediate array that is promptly thrown away. Instead it is faster to mutate when we know it's safe.
# bad
array = ["a", "b", "c"]
array.compact.flatten.map { |x| x.downcase }
# good.
array = ["a", "b", "c"]
array.compact!
array.flatten!
array.map! { |x| x.downcase }
array