Skip to content

Commit f3c1936

Browse files
committed
Tweak array construction in normalize_keys
This results in a performance increase, and reduced object allocation, when normalizing keys - ```ruby report = Benchmark.ips do |x| x.report("concat") do ex = [] ex.concat ['foo'] ex.concat ['bar', 'baz'] ex.concat ['quix'] ex end x.report("splat") do [ *['foo'], *['bar', 'baz'], *['quix'] ] end x.compare! Warming up -------------------------------------- concat 129.103k i/100ms splat 243.963k i/100ms Calculating ------------------------------------- concat 1.176M (± 9.7%) i/s - 5.810M in 5.009251s splat 2.435M (±11.8%) i/s - 11.954M in 5.019257s Comparison: splat: 2434746.6 i/s concat: 1176432.3 i/s - 2.07x (± 0.00) slower ```
1 parent a1dc424 commit f3c1936

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

lib/i18n.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ def with_locale(tmp_locale = nil)
338338
def normalize_keys(locale, key, scope, separator = nil)
339339
separator ||= I18n.default_separator
340340

341-
keys = []
342-
keys.concat normalize_key(locale, separator)
343-
keys.concat normalize_key(scope, separator)
344-
keys.concat normalize_key(key, separator)
345-
keys
341+
[
342+
*normalize_key(locale, separator),
343+
*normalize_key(scope, separator),
344+
*normalize_key(key, separator)
345+
]
346346
end
347347

348348
# Returns true when the passed locale, which can be either a String or a

0 commit comments

Comments
 (0)