Created
December 20, 2014 14:50
-
-
Save jgaskins/6a46816a73fae134f4c8 to your computer and use it in GitHub Desktop.
Hashie vs OpenStruct vs PORO performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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