summaryrefslogtreecommitdiff
path: root/benchmarks/permissive_respond_to.rb
blob: e7e894e991d01b0f9e6495bbdf5bf7ef6a6459cd (plain)
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
#!/usr/bin/env ruby

$LOAD_PATH.unshift File.expand_path(File.join('..', 'lib'), __dir__)

require 'hashie'
require 'benchmark/ips'
require 'benchmark/memory'

permissive = Class.new(Hashie::Mash)

Benchmark.memory do |x|
  x.report('Default') {}
  x.report('Make permissive') do
    permissive.include Hashie::Extensions::Mash::PermissiveRespondTo
  end
end

class PermissiveMash < Hashie::Mash
  include Hashie::Extensions::Mash::PermissiveRespondTo
end

Benchmark.ips do |x|
  x.report('Mash.new') { Hashie::Mash.new(a: 1) }
  x.report('Permissive.new') { PermissiveMash.new(a: 1) }

  x.compare!
end

Benchmark.ips do |x|
  x.report('Mash#attr=') { Hashie::Mash.new.a = 1 }
  x.report('Permissive#attr=') { PermissiveMash.new.a = 1 }

  x.compare!
end

mash = Hashie::Mash.new(a: 1)
permissive = PermissiveMash.new(a: 1)

Benchmark.ips do |x|
  x.report('Mash#attr= x2') { mash.a = 1 }
  x.report('Permissive#attr= x2') { permissive.a = 1 }

  x.compare!
end