https://github.com/jekyll/jekyll
Raw File
Tip revision: 053acd31ac69f915d2e5035dac8a6c6e71ad413c authored by olivia on 25 January 2018, 18:34:49 UTC
Release :gem: 3.7.2
Tip revision: 053acd3
flat-map
#!/usr/bin/env ruby
require 'benchmark/ips'

enum = (0..50).to_a
nested = enum.map { |i| [i] }

def do_thing(blah)
  blah * 1
end

Benchmark.ips do |x|
  x.report('.map.flatten with nested arrays') { nested.map { |i| do_thing(i) }.flatten(1) }
  x.report('.flat_map with nested arrays')    { nested.flat_map { |i| do_thing(i) } }

  x.report('.map.flatten with no nested arrays') { enum.map { |i| do_thing(i) }.flatten(1) }
  x.report('.flat_map with no nested arrays')    { enum.flat_map { |i| do_thing(i) } }
end
back to top