Last active
August 29, 2015 14:07
-
-
Save huacnlee/96374e46bb35c7351878 to your computer and use it in GitHub Desktop.
Benchmark for ActiveView asset_path helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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