Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active June 11, 2020 14:27
Show Gist options
  • Select an option

  • Save hopsoft/ae361319c54bbcb4f8e2 to your computer and use it in GitHub Desktop.

Select an option

Save hopsoft/ae361319c54bbcb4f8e2 to your computer and use it in GitHub Desktop.

Revisions

  1. hopsoft revised this gist Dec 26, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion summary.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    |strategy |time |degradation|
    ------------------------------------------
    |:------------------|-------:|----------:|
    |check for nil |0.040230| 1.00 |
    |check respond_to |0.101780| 2.529 |
    |rescue |2.103482|52.286 |
    |active_support try |0.151765| 3.772 |
    |safe navigation |0.040369| 1.003 |

  2. hopsoft revised this gist Dec 26, 2015. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions summary.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    strategy |time |degradation
    check for nil |0.040230| 1.00
    check respond_to |0.101780| 2.529
    rescue |2.103482|52.286
    active_support try |0.151765| 3.772
    safe navigation |0.040369| 1.003
    |strategy |time |degradation|
    ------------------------------------------
    |check for nil |0.040230| 1.00 |
    |check respond_to |0.101780| 2.529 |
    |rescue |2.103482|52.286 |
    |active_support try |0.151765| 3.772 |
    |safe navigation |0.040369| 1.003 |
  3. hopsoft revised this gist Dec 26, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions summary.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    strategy |time |degradation
    check for nil |0.040230| 1.00
    check respond_to |0.101780| 2.529
    rescue |2.103482|52.286
    active_support try |0.151765| 3.772
    safe navigation |0.040369| 1.003
  4. hopsoft renamed this gist Dec 26, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. hopsoft created this gist Dec 26, 2015.
    6 changes: 6 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    user system total real
    check for nil: 0.040000 0.000000 0.040000 ( 0.040230)
    check respond_to: 0.100000 0.000000 0.100000 ( 0.101780)
    rescue: 2.080000 0.020000 2.100000 ( 2.103482)
    active_support try: 0.150000 0.000000 0.150000 ( 0.151765)
    safe navigation: 0.040000 0.000000 0.040000 ( 0.040369)
    27 changes: 27 additions & 0 deletions test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    require "benchmark"
    require "active_support/all"

    Benchmark.bm do |x|
    count = 1_000_000
    label_size = 20

    x.report "check for nil:".rjust(label_size) do
    count.times { nil && nil.length }
    end

    x.report "check respond_to:".rjust(label_size) do
    count.times { nil.length if nil.respond_to?(:length) }
    end

    x.report "rescue:".rjust(label_size) do
    count.times { nil.length rescue nil }
    end

    x.report "active_support try:".rjust(label_size) do
    count.times { nil.try(:length) }
    end

    x.report "safe navigation:".rjust(label_size) do
    count.times { nil&.length }
    end
    end