Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created November 10, 2025 00:02
Show Gist options
  • Select an option

  • Save SamSaffron/e708b25685f93a99233b9f450ce82ff0 to your computer and use it in GitHub Desktop.

Select an option

Save SamSaffron/e708b25685f93a99233b9f450ce82ff0 to your computer and use it in GitHub Desktop.
env_lookup.rb
# frozen_string_literal: true
require "benchmark"
require "memory_profiler"
require "active_support"
require "active_support/core_ext/object/blank"
def test_fast
@cache = ENV["TESTING"].presence if !defined?(@cache)
@cache
end
def test_slow
ENV["TESTING"].presence
end
ENV["TESTING"] = "123"
MemoryProfiler.report { 100.times { test_fast } }.pretty_print
# Allocated String Report
# -----------------------------------
# 1 "123"
MemoryProfiler.report { 100.times { test_slow } }.pretty_print
# Allocated String Report
# -----------------------------------
# 100 "123"
Benchmark.bmbm do |x|
x.report("fast") do
i = 1000
while i > 0
test_fast
i -= 1
end
end
x.report("slow") do
i = 1000
while i > 0
test_slow
i -= 1
end
end
end
# user system total real
# fast 0.000020 0.000000 0.000020 ( 0.000020)
# slow 0.000202 0.000000 0.000202 ( 0.000201)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment