Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active August 29, 2015 14:07
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 huacnlee/96374e46bb35c7351878 to your computer and use it in GitHub Desktop.
Save huacnlee/96374e46bb35c7351878 to your computer and use it in GitHub Desktop.
Benchmark for ActiveView asset_path helper
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}i
def asset_path(source, options = {})
source = source.to_s
return "" unless source.present?
return source if source =~ URI_REGEXP
tail = source[/([\?#].+)$/] || ''
source.chomp!(tail)
if extname = compute_asset_extname(source, options)
source.concat(extname)
end
unless source.start_with?('/'.freeze)
source = compute_asset_path(source, options)
end
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
if relative_url_root
source = File.join(relative_url_root, source) unless source.starts_with?(relative_url_root)
end
if host = compute_asset_host(source, options)
source = File.join(host, source)
end
source.concat(tail)
end
def asset_path(source, options = {})
source = source.to_s
return "" unless source.present?
return source if source =~ URI_REGEXP
tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
if extname = compute_asset_extname(source, options)
source = "#{source}#{extname}"
end
if source[0] != ?/
source = compute_asset_path(source, options)
end
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
if relative_url_root
source = File.join(relative_url_root, source) unless source.starts_with?("#{relative_url_root}/")
end
if host = compute_asset_host(source, options)
source = File.join(host, source)
end
"#{source}#{tail}"
end
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'benchmark/ips'
require 'action_view'
require 'memory_profiler'
class Foo
include ActionView::Helpers::AssetUrlHelper
include ActionView::Helpers::AssetTagHelper
end
@foo = Foo.new
puts "=============== Before"
MemoryProfiler.report do
@foo.asset_path_before("/application.txt?v=1111")
end.pretty_print
puts "=============== After"
MemoryProfiler.report do
@foo.asset_path("/application.txt?v=1111")
end.pretty_print
# exit
Benchmark.ips do |x|
x.report("asset_path before") {
@foo.asset_path_before("application.js?v=1111")
}
x.report("asset_path after") {
@foo.asset_path("application.js?v=1111")
}
x.compare!
end
## Before
Total allocated 13
Total retained 0
## After
Total allocated 4
Total retained 0
## Benchmark
Calculating -------------------------------------
asset_path before 9350 i/100ms
asset_path after 10956 i/100ms
-------------------------------------------------
asset_path before 114269.7 (±6.0%) i/s - 579700 in 5.091220s
asset_path after 137230.6 (±6.1%) i/s - 690228 in 5.048627s
Comparison:
asset_path after: 137230.6 i/s
asset_path before: 114269.7 i/s - 1.20x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment