Skip to content

Instantly share code, notes, and snippets.

@beanieboi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beanieboi/174fd55d548830167cfa to your computer and use it in GitHub Desktop.
Save beanieboi/174fd55d548830167cfa to your computer and use it in GitHub Desktop.

The Benchmark

Calculating -------------------------------------
       map + flatten      1927 i/100ms
            flat_map      2603 i/100ms
-------------------------------------------------
       map + flatten    19876.1 (±9.2%) i/s -     100204 in   5.086854s
            flat_map 74241008.8 (±18.8%) i/s -  312185599 in   4.754152s

Comparison:
            flat_map: 74241008.8 i/s
       map + flatten:    19876.1 i/s - 3735.20x slower

The Benchmark Code

Benchmark.ips do |x|
  x.report("map + flatten") do
    (1..100).to_a.map { |i| [i.to_s] }.flatten
  end

  x.report("flat_map") do |times|
    (1..100).to_a.flat_map { |i| [i.to_s] }
  end

  x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment