Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pathname.join (Rails.root.join) vs String Interpolation benchmark
# frozen_string_literal: true
require "benchmark/ips"
require "pathname"
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
root = Pathname.new("/home/foo/bar/app")
x.report("pathname join") do |times|
i = 0
while i < times
var = root.join("tmp", "path", "file").to_s
i += 1
end
end
x.report("string iterp.") do |times|
i = 0
while i < times
var = "#{root}/tmp/path/file"
i += 1
end
end
x.compare!
end
Warming up --------------------------------------
pathname join 5.643k i/100ms
string iterp. 252.100k i/100ms
Calculating -------------------------------------
pathname join 57.791k (± 4.4%) i/s - 293.436k in 5.086740s
string iterp. 3.994M (± 2.1%) i/s - 20.168M in 5.051901s
Comparison:
string iterp.: 3993877.9 i/s
pathname join: 57791.3 i/s - 69.11x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.