Skip to content

Instantly share code, notes, and snippets.

@tomfakes
Last active September 12, 2019 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfakes/b492fa333307a08cfa502e3a6d286df7 to your computer and use it in GitHub Desktop.
Save tomfakes/b492fa333307a08cfa502e3a6d286df7 to your computer and use it in GitHub Desktop.
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