Skip to content

Files

Latest commit

 

History

History
27 lines (20 loc) · 593 Bytes

Performance-ChainArrayAllocation.md

File metadata and controls

27 lines (20 loc) · 593 Bytes

Pattern: Extra array allocation

Issue: -

Description

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.

Examples

# 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

Further Reading