Skip to content

Commit

Permalink
Generate js_locale_data using JSON.dump
Browse files Browse the repository at this point in the history
We generate some fairly large JSON here, and Rails's to_json is a fair
bit slower than just JSON.dump due to dealing with ActiveRecord and
other objects.

           I18n.t()  177.064k (± 4.5%) i/s
   I18n.t().to_json  155.028  (± 1.3%) i/s
JSON.dump(I18n.t())    1.045k (± 1.1%) i/s

So JSON.dump is ~5x faster faster than using to_json.
  • Loading branch information
jhawthorn committed Oct 18, 2017
1 parent 528d911 commit 2f43603
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions backend/app/views/spree/admin/shared/_js_locale_data.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,42 @@
var Spree = {}
}
Spree.translations = <%==
I18n.t("spree").merge({
date_picker: Spree.t(:js_format,
scope: 'date_picker',
default: 'yy/mm/dd'),
abbr_day_names: I18n.t(:abbr_day_names, scope: :date),
destroy: Spree.t(:destroy, scope: :actions),
edit: Spree.t(:edit, scope: :actions),
save: Spree.t(:save, scope: :actions),
cancel: Spree.t(:cancel, scope: :actions),
first_day: Spree.t(:first_day,
scope: 'date_picker',
default: 0).to_i,
item_stock_placeholder: Spree.t(:choose_location),
month_names: I18n.t(:month_names, scope: :date).reject(&:blank?),
currency_separator: I18n.t('number.currency.format.separator'),
currency_delimiter: I18n.t('number.currency.format.delimiter'),
activerecord: I18n.t('activerecord')
}).to_json
JSON.dump(
I18n.t("spree").merge({
date_picker: Spree.t(:js_format,
scope: 'date_picker',
default: 'yy/mm/dd'),
abbr_day_names: I18n.t(:abbr_day_names, scope: :date),
destroy: Spree.t(:destroy, scope: :actions),
edit: Spree.t(:edit, scope: :actions),
save: Spree.t(:save, scope: :actions),
cancel: Spree.t(:cancel, scope: :actions),
first_day: Spree.t(:first_day,
scope: 'date_picker',
default: 0).to_i,
item_stock_placeholder: Spree.t(:choose_location),
month_names: I18n.t(:month_names, scope: :date).reject(&:blank?),
currency_separator: I18n.t('number.currency.format.separator'),
currency_delimiter: I18n.t('number.currency.format.delimiter'),
activerecord: I18n.t('activerecord')
}))
%>;

Spree.currencyInfo = <%==
Money::Currency.all.map { |c|
format =
if c.symbol == "" || c.symbol_first
"%s%v"
else
"%v %s"
end
[c.id.to_s.upcase, [
c.symbol || "¤",
c.exponent,
format
]]
}.to_h.to_json
JSON.dump(
Money::Currency.all.map { |c|
format =
if c.symbol == "" || c.symbol_first
"%s%v"
else
"%v %s"
end
[c.id.to_s.upcase, [
c.symbol || "¤",
c.exponent,
format
]]
}.to_h)
%>;
</script>
<script data-hook='admin-custom-translations'>
Expand Down

0 comments on commit 2f43603

Please sign in to comment.