discourse / mini_mime

minimal mime type library
MIT License
95 stars 34 forks source link

replace sub #14

Closed ahorek closed 6 years ago

ahorek commented 6 years ago
require 'benchmark/ips'

extension = '.zip'
Benchmark.ips do |x|
  x.report('sub') do
    extension.dup.sub!('.'.freeze, ''.freeze)
  end
  x.report('[]') do
    extension.dup[1..-1]
  end
  x.compare!
end
Warming up --------------------------------------
                 sub    74.400k i/100ms
                  []   103.522k i/100ms
Calculating -------------------------------------
                 sub      1.217M (± 8.2%) i/s -      6.101M in   5.056085s
                  []      1.929M (± 6.3%) i/s -      9.628M in   5.022440s

Comparison:
                  []:  1928646.3 i/s
                 sub:  1217186.0 i/s - 1.58x  slower
SamSaffron commented 6 years ago

nice!

only concern is ... can we confirm that all the extensions there start with . ?

ahorek commented 6 years ago

the result from File.extname should always start with a single "." or be empty.

here's the related code https://github.com/ruby/ruby/blob/081df640833fb4871048bcecffe61f110facd352/file.c#L4436

AFAIK this change should be safe

SamSaffron commented 6 years ago

looks good to me then! thanks