https://github.com/jekyll/jekyll
Raw File
Tip revision: 2b4a3c008ddbdc6d7d8381f2ba6a031ec5efdc45 authored by Parker Moore on 18 November 2015, 06:19:45 UTC
Release :gem: 3.0.1
Tip revision: 2b4a3c0
flat-map
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