Skip to content

Instantly share code, notes, and snippets.

@philiplambok
Created August 14, 2021 04:12
Show Gist options
  • Save philiplambok/5fdd027b3ba7581fbb68829cac88ae5d to your computer and use it in GitHub Desktop.
Save philiplambok/5fdd027b3ba7581fbb68829cac88ae5d to your computer and use it in GitHub Desktop.
local vs let vs instance variable
# frozen_string_literal: true

ENV['RAILS_ENV'] ||= 'test'

require 'benchmark/ips'
require File.expand_path('../config/environment', __dir__)
require 'rspec/rails'

RSpec::Core::Runner.autorun

RSpec.configure do |c|
  c.output_stream = StringIO.new
end

def calculating_something
  i = 0
  100.times do |index|
    i += index
  end
  i
end

def using_local(loops = 1)
  RSpec.describe 'using local variable' do
    loops.times do |x|
      it "returns expected #{x}" do
        result = calculating_something
        expect(result).to eq 4950
      end
    end
  end
end

def using_let(loops = 1)
  RSpec.describe 'using let variable' do
    let(:result) { calculating_something }

    loops.times do |x|
      it "returns expected #{x}" do
        expect(result).to eq 4950
      end
    end
  end
end

def using_instance_variable(loops = 1)
  RSpec.describe 'using instance variable' do
    before(:all) do
      @result ||= calculating_something
    end

    loops.times do |x|
      it "returns expected #{x}" do
        expect(@result).to eq 4950
      end
    end
  end
end

Benchmark.ips do |bench|
  bench.confidence = 100
  bench.report('using local') { using_local }
  bench.report('using instance_variable') { using_instance_variable }
  bench.report('using let') { using_let }
  bench.compare!
end

Benchmark.ips do |bench|
  bench.confidence = 100
  bench.report('using local') { using_local(10) }
  bench.report('using instance_variable') { using_instance_variable(10) }
  bench.report('using let') { using_let(10) }
  bench.compare!
end

# Warming up --------------------------------------
#          using local    91.000  i/100ms
# using instance_variable
#                        125.000  i/100ms
#            using let    85.000  i/100ms
# Calculating -------------------------------------
#          using local    619.861  (±27.7%) i/s -      2.912k in   5.098755s
# using instance_variable
#                         475.003  (±27.2%) i/s -      2.250k in   5.017962s
#            using let    588.896  (±25.6%) i/s -      2.805k in   5.125193s

# Comparison:
#          using local:      619.9 i/s
#            using let:      588.9 i/s - same-ish: difference falls within error
# using instance_variable:      475.0 i/s - same-ish: difference falls within error

# Warming up --------------------------------------
#          using local    33.000  i/100ms
# using instance_variable
#                         29.000  i/100ms
#            using let    33.000  i/100ms
# Calculating -------------------------------------
#          using local    221.986  (±15.3%) i/s -      1.089k in   5.042750s
# using instance_variable
#                         228.964  (±17.5%) i/s -      1.131k in   5.128995s
#            using let    240.300  (±18.3%) i/s -      1.188k in   5.143719s

# Comparison:
#            using let:      240.3 i/s
# using instance_variable:      229.0 i/s - same-ish: difference falls within error
#          using local:      222.0 i/s - same-ish: difference falls within error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment