提交 707dfcdb 编写于 作者: A Aaron Patterson

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
上级 fea1cdcf
......@@ -89,6 +89,7 @@ def camelize(term, uppercase_first_letter = true)
#
# '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')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册