data = {
foo: 'foo',
great_grandparent: {
grandparent: {
parent: {
child: {
foo: 'foo'}
}
}
}
}
I18n.backend.store_translations('en', data)
Benchmark.ips do |b|
b.time = 1
# auto warmup causes high variability
b.warmup = 0
# manual warmup to load translations into backend
I18n.t(:bar)
b.report("key match (1 lookup)") do
I18n.t(:foo)
end
b.report("fallback to default (2 lookups)") do
I18n.t(:miss, default: :foo)
end
b.report("fallback to default (3 lookups)") do
I18n.t(:miss, default: [:miss2, :foo])
end
b.report("fallback to default (4 lookups)") do
I18n.t(:miss, default: [:miss2, :miss3, :foo])
end
b.report("fallback to default (5 lookups)") do
I18n.t(:miss, default: [:miss2, :miss3, :miss4, :foo])
end
b.report("nested defaults (5 lookups)") do
I18n.t(:miss, default: [
:"great_grandparent.grandparent.parent.child.miss",
:"great_grandparent.grandparent.parent.miss",
:"great_grandparent.grandparent.miss",
:"great_grandparent.grandparent.parent.child.foo",
])
end
b.compare!
end
-------------------------------------------------
key match (1 lookup) 43.244k (±10.5%) i/s - 10.659k
fallback to default (2 lookups)
21.227k (±10.7%) i/s - 6.903k
fallback to default (3 lookups)
14.405k (±13.2%) i/s - 5.485k in 0.621587s
fallback to default (4 lookups)
11.409k (±10.4%) i/s - 4.560k
fallback to default (5 lookups)
9.422k (± 9.8%) i/s - 4.084k
nested defaults (5 lookups)
8.917k (± 9.7%) i/s - 3.860k in 0.804303s
Comparison:
key match (1 lookup): 43243.6 i/s
fallback to default (2 lookups): 21226.7 i/s - 2.04x slower
fallback to default (3 lookups): 14404.7 i/s - 3.00x slower
fallback to default (4 lookups): 11408.8 i/s - 3.79x slower
fallback to default (5 lookups): 9421.7 i/s - 4.59x slower
nested defaults (5 lookups): 8917.1 i/s - 4.85x slower