Sha256: b58105cfd27be8a19b6b03edfc1c42d1a231e76d9c8ef31164c673f2c27965a7

Contents?: true

Size: 1.3 KB

Versions: 11

Compression:

Stored size: 1.3 KB

Contents

require 'set'
require 'date'
require 'benchmark/ips'

ALLOWED_IN_KEYS = [
  ::String,
  ::Symbol,
  ::Numeric,
  ::TrueClass,
  ::FalseClass,
  ::NilClass,
  ::Integer,
  ::Float,
  ::Date,
  ::DateTime,
  ::Time,
].to_set
parts = RUBY_VERSION.split('.').map(&:to_i)
unless parts[0] >= 2 and parts[1] >= 4
  ALLOWED_IN_KEYS << ::Fixnum
  ALLOWED_IN_KEYS << ::Bignum  
end

EXAMPLES = [
  'hi',
  :there,
  123,
  123.54,
  1e99,
  123456789 ** 2,
  1e999,
  true,
  false,
  nil,
  Date.new(2015,1,1),
  Time.now,
  DateTime.now,
  Mutex,
  Mutex.new,
  Benchmark,
  { hi: :world },
  [[]],
  Fixnum,
  Struct,
  Struct.new(:a),
  Struct.new(:a).new(123)
]
EXAMPLES.each do |example|
  puts "#{example} -> #{example.class}"
end

puts

[
  Date.new(2015,1,1),
  Time.now,
  DateTime.now,
].each do |x|
    puts x.to_s
end

puts

EXAMPLES.each do |example|
  a = ALLOWED_IN_KEYS.any? { |thing| example.is_a?(thing) }
  b = ALLOWED_IN_KEYS.include? example.class
  unless a == b
    raise "#{example.inspect}: #{a.inspect} vs #{b.inspect}"
  end
end

Benchmark.ips do |x|
  x.report("any") do
    example = EXAMPLES.sample
    y = ALLOWED_IN_KEYS.any? { |thing| example.is_a?(thing) }
    a = 1
    y
  end

  x.report("include") do
    example = EXAMPLES.sample
    y = ALLOWED_IN_KEYS.include? example.class
    a = 1
    y
  end

end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
lock_and_cache-7.0.0 benchmarks/allowed_in_keys.rb
lock_and_cache-6.0.1 benchmarks/allowed_in_keys.rb
lock_and_cache-6.0.0 benchmarks/allowed_in_keys.rb
lock_and_cache-5.0.0 benchmarks/allowed_in_keys.rb
lock_and_cache_msgpack-4.1.0 benchmarks/allowed_in_keys.rb
lock_and_cache_msgpack-4.0.7.pre4 benchmarks/allowed_in_keys.rb
lock_and_cache_msgpack-4.0.7.pre3 benchmarks/allowed_in_keys.rb
lock_and_cache_msgpack-4.0.7.pre2 benchmarks/allowed_in_keys.rb
lock_and_cache_msgpack-4.0.7.pre1 benchmarks/allowed_in_keys.rb
lock_and_cache-4.0.6 benchmarks/allowed_in_keys.rb
lock_and_cache-4.0.5 benchmarks/allowed_in_keys.rb