Skip to content

Instantly share code, notes, and snippets.

@guiferrpereira
Last active August 18, 2017 23:28
Show Gist options
  • Save guiferrpereira/b9ac3e5538ebc54c9e3d028700098ed4 to your computer and use it in GitHub Desktop.
Save guiferrpereira/b9ac3e5538ebc54c9e3d028700098ed4 to your computer and use it in GitHub Desktop.
concat vs flat_map
require 'benchmark'
require 'action_view'
# include ActionView::Context
# include ActionView::Helpers::TextHelper
a = 'a'
b = 'b'
arr = %w(a b c d e f g h i)
Benchmark.bmbm(7) do |bm|
bm.report('concat') do
1000.times do
cards = []
arr.each_slice(6) do |a|
cards.concat(a)
end
end
end
bm.report('flat_map') do
1000.times do
cards = []
cards = arr.each_slice(6).flat_map do |a|
a
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment