Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created December 20, 2014 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jgaskins/6a46816a73fae134f4c8 to your computer and use it in GitHub Desktop.
Save jgaskins/6a46816a73fae134f4c8 to your computer and use it in GitHub Desktop.
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