SHARE
TWEET

Untitled

a guest Jan 5th, 2014 38 Never
  1. require 'benchmark'
  2.  
  3. n = 1_000_000
  4. Benchmark.bm(20) do |x|
  5.   x.report("chained methods:") do
  6.     n.times do
  7.       ['coconut', 'lemon', 'banana', 'apple'].select { |e| e.size > 5 }
  8.                                              .map { |e| e.upcase }
  9.     end
  10.   end
  11.  
  12.   x.report("only reduce:") do
  13.     n.times do
  14.       ['coconut', 'lemon', 'banana', 'apple'].reduce([]) do |acc, e|
  15.         acc << e.upcase if e.size > 5
  16.         acc
  17.       end
  18.     end
  19.   end
  20. end
RAW Paste Data
Top