Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@lvl0nax @JuanitoFatas
30 lines (24 sloc) 638 Bytes
require "benchmark/ips"
ENUM = (1..100)
ORIGINAL_HASH = { foo: "foo" }
def fast
ENUM.inject([]) do |accumulator, element|
accumulator << ({ bar: element }.merge!(ORIGINAL_HASH){ |_key, left, _right| left })
end
end
def slow
ENUM.inject([]) do |accumulator, element|
accumulator << ORIGINAL_HASH.merge(bar: element)
end
end
def slow_dup
ENUM.inject([]) do |accumulator, element|
accumulator << ORIGINAL_HASH.dup.merge!(bar: element)
end
end
Benchmark.ips do |x|
x.report("{}#merge!(Hash) do end") { fast }
x.report("Hash#merge({})") { slow }
x.report("Hash#dup#merge!({})") { slow_dup }
x.compare!
end
You can’t perform that action at this time.