From 1f831fefe226ed42e89c0b58076f5b6d66588d7e Mon Sep 17 00:00:00 2001 From: schneems Date: Sun, 26 Jul 2015 14:37:21 -0500 Subject: Don't allocate array when not necessary In the `tag_options` method an array is used to build up elements, then `Array#*` (which is an alias for `Array#join` is called to turn the array into a string. Instead of allocating an array to build a string, we can build the string we want from the beginning. Saved: 121,743 bytes 893 objects --- actionview/lib/action_view/helpers/tag_helper.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/helpers/tag_helper.rb b/actionview/lib/action_view/helpers/tag_helper.rb index a821bd9543..30af09cbac 100644 --- a/actionview/lib/action_view/helpers/tag_helper.rb +++ b/actionview/lib/action_view/helpers/tag_helper.rb @@ -149,19 +149,20 @@ module ActionView def tag_options(options, escape = true) return if options.blank? - attrs = [] + output = "" + sep = " ".freeze options.each_pair do |key, value| if TAG_PREFIXES.include?(key) && value.is_a?(Hash) value.each_pair do |k, v| - attrs << prefix_tag_option(key, k, v, escape) + output << sep + prefix_tag_option(key, k, v, escape) end elsif BOOLEAN_ATTRIBUTES.include?(key) - attrs << boolean_tag_option(key) if value + output << sep + boolean_tag_option(key) if value elsif !value.nil? - attrs << tag_option(key, value, escape) + output << sep + tag_option(key, value, escape) end end - " ".freeze + attrs * ' '.freeze unless attrs.empty? + output unless output.empty? end def prefix_tag_option(prefix, key, value, escape) -- cgit v1.2.3