Skip to content

Instantly share code, notes, and snippets.

@havenwood

havenwood/[].rb Secret

Last active March 24, 2016 16:44
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 havenwood/6e21d98015aab10dcb8a to your computer and use it in GitHub Desktop.
Save havenwood/6e21d98015aab10dcb8a to your computer and use it in GitHub Desktop.
Exceptions are not for flow control!
>> Benchmark.ips do |i|
| nodes = []
|
| i.report('lonely') do
| nodes&.first
| end
|
| i.report('if') do
| nodes.first if nodes
| end
|
| i.report('rescue') do
| nodes.first rescue nil
| end
| end
Warming up --------------------------------------
lonely 166.968k i/100ms
if 167.364k i/100ms
rescue 171.235k i/100ms
Calculating -------------------------------------
lonely 11.652M (± 6.5%) i/s - 58.105M
if 11.139M (± 7.3%) i/s - 55.397M
rescue 11.521M (± 8.2%) i/s - 57.192M
>> Benchmark.ips do |i|
| nodes = nil
|
| i.report('lonely') do
| nodes&.first
| end
|
| i.report('if') do
| nodes.first if nodes
| end
|
| i.report('rescue') do
| nodes.first rescue nil
| end
| end
Warming up --------------------------------------
lonely 169.405k i/100ms
if 170.775k i/100ms
rescue 1.177k i/100ms
Calculating -------------------------------------
lonely 13.127M (± 5.8%) i/s - 65.390M
if 13.091M (± 6.8%) i/s - 65.236M
rescue 12.152k (±11.3%) i/s - 61.204k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment