Skip to content

Instantly share code, notes, and snippets.

@tomascco
Created November 17, 2020 18:33
Show Gist options
  • Save tomascco/2999e1fe5febe689a861595968d47797 to your computer and use it in GitHub Desktop.
Save tomascco/2999e1fe5febe689a861595968d47797 to your computer and use it in GitHub Desktop.
blocks vs lambda
require 'benchmark/ips'
Lambda = ->n { n.even? }
Benchmark.ips do |x|
x.report('map with lambda literal') { (1..1_000).map(&->n { n.even? }) }
x.report('map with lambda constant') { (1..1_000).map(&Lambda) }
x.report('map with block') { (1..1_000).map { |n| n.even? } }
x.report('map with &') { (1..1_000).map(&:even?) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment