Skip to content

Commit

Permalink
Lower case normalization benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 26, 2023
1 parent 2c492d3 commit 586aa14
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/header-lowercase/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'benchmark/ips'

class NormalizedHeaders
def initialize(fields)
@fields = fields
end

def [](key)
@fields[key.downcase]
end
end

class Headers
def initialize(fields)
@fields = fields
end

def [](key)
@fields[key]
end
end

FIELDS = {
'content-type' => 'text/html',
'content-length' => '127889',
'accept-ranges' => 'bytes',
'date' => 'Tue, 14 Jul 2015 22:00:02 GMT',
'via' => '1.1 varnish',
'age' => '0',
'connection' => 'keep-alive',
'x-served-by' => 'cache-iad2125-IAD',
}

NORMALIZED_HEADERS = NormalizedHeaders.new(FIELDS)
HEADERS = Headers.new(FIELDS)

Benchmark.ips do |x|
x.report('NormalizedHeaders[Content-Type]') { NORMALIZED_HEADERS['Content-Type'] }
x.report('NormalizedHeaders[content-type]') { NORMALIZED_HEADERS['content-type'] }
x.report('Headers') { HEADERS['content-type'] }

x.compare!
end

0 comments on commit 586aa14

Please sign in to comment.