Skip to content

Instantly share code, notes, and snippets.

@mtkd
Created December 25, 2015 11:56
Show Gist options
  • Select an option

  • Save mtkd/c0e40a0d367fd8488b30 to your computer and use it in GitHub Desktop.

Select an option

Save mtkd/c0e40a0d367fd8488b30 to your computer and use it in GitHub Desktop.

Revisions

  1. mtkd created this gist Dec 25, 2015.
    15 changes: 15 additions & 0 deletions safe_navigator_cost.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Test perf difference between try and safe navigator in Ruby 2.3.0

    require 'benchmark'
    require 'active_support'
    require 'active_support/core_ext/object'

    Benchmark.bm do |x|
    obj = 0
    x.report { 10000.times do; obj.try(:to_s); end; }
    x.report { 10000.times do; obj&.to_s; end; }
    end

    # user system total real
    # 0.010000 0.000000 0.010000 ( 0.005675)
    # 0.000000 0.000000 0.000000 ( 0.000844)