Created
June 20, 2019 19:02
-
-
Save EdwardDiehl/1b2088883a6a197b2f03e8f601586490 to your computer and use it in GitHub Desktop.
arrays transformations benchmark
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
# source: https://github.com/rails/rails/pull/33806#issuecomment-431908935 | |
require "benchmark/inputs" | |
INPUTS = [ | |
[1, 2, 3], | |
[1, nil, 2, nil, 3, nil], | |
[nil, nil, nil], | |
] | |
Benchmark.inputs(INPUTS) do |job| | |
job.dup_inputs = true | |
job.report("lazy"){|array| array.flatten.compact.lazy.uniq.to_a } | |
job.report("normal"){|array| array.flatten.uniq.compact } | |
job.report("bang!"){|array| array.flatten!; array.uniq!; array.compact! } | |
job.report("tap(&:bang!)"){|array| array.tap(&:flatten!).tap(&:uniq!).tap(&:compact!) } | |
job.report("tap{|a| a.bang! * 3 }"){|array| array.tap{|a| a.flatten!; a.uniq!; a.compact! } } | |
job.report("tap{|a| a.bang! } * 3"){|array| array.tap{|a| a.flatten! }.tap{|a| a.uniq! }.tap{|a| a.compact! } } | |
job.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment