Skip to content

Commit

Permalink
Tweak array construction in normalize_keys
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
codealchemy committed Feb 3, 2022
1 parent a1dc424 commit f3c1936
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ def with_locale(tmp_locale = nil)
def normalize_keys(locale, key, scope, separator = nil)
separator ||= I18n.default_separator

keys = []
keys.concat normalize_key(locale, separator)
keys.concat normalize_key(scope, separator)
keys.concat normalize_key(key, separator)
keys
[
*normalize_key(locale, separator),
*normalize_key(scope, separator),
*normalize_key(key, separator)
]
end

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

0 comments on commit f3c1936

Please sign in to comment.