MimeType
MimeType is a library that sacrifices a few MB of memory for the sake of being super fast at getting your MimeType information. We would rather use 1MB of memory, and get twice (if not more) the speed, than rely on other methods of caching which don't perform nearly as fast.
Installation
gem "mimetype"
Usage
require "mimetype"
MimeType.lookup_by_extension(".jpg")
MimeType.lookup_by_content_type("text/plain")
MimeType.lookup_by_filename(Pathutil.new("hello.txt"))
MimeType.lookup_by_filename(File.new("hello.txt"))
MimeType.lookup_by_filename("hello.txt")
Performance
MimeType: 23397 obj @ 1670599 bytes w/ 9169 obj @ 573112 bytes retained
MIME::Type: 96527 obj @ 7606239 bytes w/ 30706 obj @ 2716382 bytes retained
MiniMime: 399 obj @ 41769 bytes w/ 54 obj @ 7229 bytes retained
Warming up --------------------------------------
MIME::TYPE by ext 27.369k i/100ms
MimeType by ext 134.058k i/100ms
MiniMime by ext 51.839k i/100ms
Calculating -------------------------------------
MIME::TYPE by ext 301.471k (± 2.5%) i/s - 3.640M in 12.081653s
MimeType by ext 2.107M (± 1.8%) i/s - 25.337M in 12.031807s
MiniMime by ext 607.654k (± 3.1%) i/s - 7.309M in 12.040407s
Comparison:
MimeType by ext: 2106589.0 i/s
MiniMime by ext: 607653.8 i/s - 3.47x slower
MIME::TYPE by ext: 301470.5 i/s - 6.99x slower
Warming up --------------------------------------
MIME::TYPE by type 23.544k i/100ms
MimeType by type 199.664k i/100ms
MiniMime by type 123.087k i/100ms
Calculating -------------------------------------
MIME::TYPE by type 243.347k (± 3.8%) i/s - 2.919M in 12.014030s
MimeType by type 3.960M (± 1.5%) i/s - 47.520M in 12.002210s
MiniMime by type 1.859M (± 2.0%) i/s - 22.402M in 12.054182s
Comparison:
MimeType by type: 3960282.0 i/s
MiniMime by type: 1859217.4 i/s - 2.13x slower
MIME::TYPE by type: 243347.1 i/s - 16.27x slower