Skip to content

Commit

Permalink
Merge pull request #17483 from pabloh/optimize_gsub_calls
Browse files Browse the repository at this point in the history
Call gsub with a Regexp instead of a String for better performance
  • Loading branch information
sgrif committed Nov 2, 2014
2 parents 50e7c01 + 861b70e commit 8b611b7
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/live.rb
Expand Up @@ -102,7 +102,7 @@ def perform_write(json, options)
end
end

message = json.gsub("\n", "\ndata: ")
message = json.gsub(/\n/, "\ndata: ")
@stream.write "data: #{message}\n\n"
end
end
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -123,7 +123,7 @@ def content_tag(name, content_or_options_with_block = nil, options = nil, escape
# cdata_section("hello]]>world")
# # => <![CDATA[hello]]]]><![CDATA[>world]]>
def cdata_section(content)
splitted = content.to_s.gsub(']]>', ']]]]><![CDATA[>')
splitted = content.to_s.gsub(/\]\]\>/, ']]]]><![CDATA[>')
"<![CDATA[#{splitted}]]>".html_safe
end

Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/template/handlers/raw.rb
Expand Up @@ -2,7 +2,7 @@ module ActionView
module Template::Handlers
class Raw
def call(template)
escaped = template.source.gsub(':', '\:')
escaped = template.source.gsub(/:/, '\:')

'%q:' + escaped + ':;'
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/sanitization.rb
Expand Up @@ -134,7 +134,7 @@ def replace_bind_variables(statement, values) #:nodoc:
raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
bound = values.dup
c = connection
statement.gsub('?') do
statement.gsub(/\?/) do
replace_bind_variable(bound.shift, c)
end
end
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/inflector/methods.rb
Expand Up @@ -73,7 +73,7 @@ def camelize(term, uppercase_first_letter = true)
string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase }
end
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }
string.gsub!('/', '::')
string.gsub!(/\//, '::')
string
end

Expand Down

0 comments on commit 8b611b7

Please sign in to comment.