aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-02-26 15:09:18 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2014-02-26 15:09:18 -0800
commit707dfcdbc36c48af3411b9de08d70757ee4e612f (patch)
tree4d727e77f5e62f72a8f5d9596b29fdcb086eeca1 /activesupport/lib
parentfea1cdcff4d50d302d8e8532432c3ab107ff816d (diff)
downloadrails-707dfcdbc36c48af3411b9de08d70757ee4e612f.tar.gz
rails-707dfcdbc36c48af3411b9de08d70757ee4e612f.tar.bz2
rails-707dfcdbc36c48af3411b9de08d70757ee4e612f.zip
speed up `underscore` in cases that don't need to do anything
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
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb1
1 files changed, 1 insertions, 0 deletions
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')