From 707dfcdbc36c48af3411b9de08d70757ee4e612f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 26 Feb 2014 15:09:18 -0800 Subject: speed up `underscore` in cases that don't need to do anything MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benchmark: Benchmark.ips do |x| x.report("home") { "home".underscore } x.report("Home") { "Home".underscore } x.report("homeBase") { "homeBase".underscore } x.report("home::base") { "home::base".underscore } end Before: Calculating ------------------------------------- home 2598 i/100ms Home 2463 i/100ms homeBase 2300 i/100ms home::base 2086 i/100ms ------------------------------------------------- home 28522.3 (±14.7%) i/s - 140292 in 5.065102s Home 29165.8 (±14.9%) i/s - 140391 in 5.000475s homeBase 26218.5 (±7.1%) i/s - 131100 in 5.030485s home::base 27712.3 (±5.9%) i/s - 139762 in 5.064077s After: Calculating ------------------------------------- home 23163 i/100ms Home 2432 i/100ms homeBase 2160 i/100ms home::base 2265 i/100ms ------------------------------------------------- home 1501614.8 (±10.2%) i/s - 7412160 in 5.009540s Home 28754.0 (±8.5%) i/s - 143488 in 5.033886s homeBase 25331.1 (±5.6%) i/s - 127440 in 5.047940s home::base 27089.9 (±5.5%) i/s - 135900 in 5.033516s --- activesupport/lib/active_support/inflector/methods.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib/active_support/inflector') diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index b642d87d76..a270c4452f 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -89,6 +89,7 @@ module ActiveSupport # # 'SSLError'.underscore.camelize # => "SslError" def underscore(camel_cased_word) + return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub('::', '/') word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') -- cgit v1.2.3