Hashie vs OpenStruct vs PORO performance
require 'hashie' | |
require 'ostruct' | |
require 'benchmark/ips' | |
class PORO | |
attr_reader :foo, :bar, :baz | |
def initialize attrs={} | |
attrs.each do |attr, value| | |
instance_variable_set "@#{attr}", value | |
end | |
end | |
end | |
attrs = { foo: 1, bar: true, baz: 'quux' } | |
hashie = Hashie::Mash.new(attrs) | |
ostruct = OpenStruct.new(attrs) | |
poro = PORO.new(attrs) | |
Benchmark.ips do |x| | |
x.report 'Hashie alloc' do | |
Hashie::Mash.new(attrs) | |
end | |
x.report 'OpenStruct alloc' do | |
OpenStruct.new(attrs) | |
end | |
x.report 'PORO alloc' do | |
PORO.new(attrs) | |
end | |
x.report 'Hashie access' do | |
hashie.foo | |
hashie.bar | |
hashie.baz | |
end | |
x.report 'OStruct access' do | |
ostruct.foo | |
ostruct.bar | |
ostruct.baz | |
end | |
x.report 'PORO access' do | |
poro.foo | |
poro.bar | |
poro.baz | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment