Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
29 lines (22 sloc) 499 Bytes
require "benchmark/ips"
HASH_WITH_SYMBOL = { fast: "ruby" }
HASH_WITH_STRING = { "fast" => "ruby" }
def fastest
HASH_WITH_SYMBOL[:fast]
end
def faster
HASH_WITH_SYMBOL.fetch(:fast)
end
def fast
HASH_WITH_STRING["fast"]
end
def slow
HASH_WITH_STRING.fetch("fast")
end
Benchmark.ips do |x|
x.report("Hash#[], symbol") { fastest }
x.report("Hash#fetch, symbol") { faster }
x.report("Hash#[], string") { fast }
x.report("Hash#fetch, string") { slow }
x.compare!
end
You can’t perform that action at this time.