Skip to content

Instantly share code, notes, and snippets.

@jordifebrer
Last active January 29, 2016 10:00
Show Gist options
  • Save jordifebrer/0b3c10ec6756095405b5 to your computer and use it in GitHub Desktop.
Save jordifebrer/0b3c10ec6756095405b5 to your computer and use it in GitHub Desktop.
Struct vs OpenStruct vs Hash benchmark
require 'benchmark'
require 'ostruct'
Benchmark.bm 10 do |bench|
bench.report "Hash: " do
10_000_000.times do { name: "John Doe", age: 32 } end
end
bench.report "Struct: " do
Person = Struct.new(:name, :age)
10_000_000.times do Person.new("John Doe", 32) end
end
bench.report "OpenStruct: " do
10_000_000.times do OpenStruct.new(name: "John Doe", age: 32) end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment