Skip to content

Instantly share code, notes, and snippets.

@charlespeach
Created September 1, 2020 11:02
Show Gist options
  • Save charlespeach/526930ca083d7b3d006cb2e6be291cc6 to your computer and use it in GitHub Desktop.
Save charlespeach/526930ca083d7b3d006cb2e6be291cc6 to your computer and use it in GitHub Desktop.
Benchmarking string concat from array of hashes
require 'benchmark/ips'
Benchmark.ips do |b|
b.config(time: 39, warmup: 2)
count = 100
ary = Array.new(count, { text: 'text' })
b.report("map") do
ary.map { |hash| hash[:text] }.join
end
b.report("inject") do
ary.inject(String.new) { |memo, hash| memo << hash[:text] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment