Last active
August 18, 2017 23:28
-
-
Save guiferrpereira/b9ac3e5538ebc54c9e3d028700098ed4 to your computer and use it in GitHub Desktop.
concat vs flat_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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