public
Created — forked from tenderlove/test.rb

  • Download Gist
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# encoding: utf-8
 
require 'rubygems'
require 'benchmark/ips'
require 'set'
 
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
BOOLEAN_MAP = Hash[ %w[ 1 on t true y yes ].product([ true ]) ]
 
def original value
if value.is_a?(String) && value.empty?
nil
else
TRUE_VALUES.include?(value)
end
end
 
def regexp value
case value
when String
value =~ /^(?:1|t(?:rue)?)$/i
when true, 1
true
else
false
end
end
 
def boolean_map(value)
BOOLEAN_MAP.fetch(value.downcase, value)
end
 
n = 50000
Benchmark.ips do |x|
x.report('original hit') {
original 'true'
}
 
x.report('original miss') {
original 'false'
}
 
x.report('regex hit') {
regexp 'true'
}
 
x.report('regex miss') {
regexp 'false'
}
 
x.report('boolean_map hit') {
original 'TrUE'
}
 
x.report('boolean_map miss') {
original 'FaLSE'
}
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.