Skip to content

Instantly share code, notes, and snippets.

@raphox
Created September 25, 2025 16:24
Show Gist options
  • Select an option

  • Save raphox/e40112a4cd55cadadb81f1115f0db88a to your computer and use it in GitHub Desktop.

Select an option

Save raphox/e40112a4cd55cadadb81f1115f0db88a to your computer and use it in GitHub Desktop.
require 'benchmark'
# Mock classes to simulate the environment
class Licensor
attr_accessor :name, :people_id
def initialize(name, people_id = nil)
@name = name
@people_id = people_id
end
def people_id
@people_id
end
def people_id_present?
!@people_id.nil?
end
end
class Statement
attr_accessor :licensor, :period_end_date
def initialize(licensor, period_end_date)
@licensor = licensor
@period_end_date = period_end_date
end
end
# Benchmarking the three approaches
licensor = Licensor.new('Easton', '12345')
statement = Statement.new(licensor, '2025-09-25')
n = 9_000_000 # Number of iterations
Benchmark.bm(10) do |x|
x.report('Array + join') do
n.times do
result = []
result << licensor.name
result << licensor.people_id if licensor.people_id
result << statement.period_end_date
result.join('_')
end
end
x.report('String concat') do
n.times do
result = licensor.name
result += "_#{licensor.people_id}" if licensor.people_id
result += "_#{statement.period_end_date}"
end
end
x.report('Unique String') do
n.times do
result = licensor.name
result += licensor.people_id if licensor.people_id
result += statement.period_end_date
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment