From 2f43603f2dfa4ba6b5c4be106a37a95224724662 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 17 Oct 2017 17:43:27 -0700 Subject: [PATCH] Generate js_locale_data using JSON.dump MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../admin/shared/_js_locale_data.html.erb | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/backend/app/views/spree/admin/shared/_js_locale_data.html.erb b/backend/app/views/spree/admin/shared/_js_locale_data.html.erb index d61a32b1c85..6ad652fdd0f 100644 --- a/backend/app/views/spree/admin/shared/_js_locale_data.html.erb +++ b/backend/app/views/spree/admin/shared/_js_locale_data.html.erb @@ -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) %>;