Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active August 29, 2015 14:21
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/e942ce9843b5c01bc7dd to your computer and use it in GitHub Desktop.
Save huacnlee/e942ce9843b5c01bc7dd to your computer and use it in GitHub Desktop.
Benchmark sprockets-rails javascript_include_tag cache
# sprockets-rails/test/test_railtie.rb
def test_benchmark_assets_tag
app.configure do
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["bar.js", "bar.css"]
end
app.initialize!
require 'benchmark/ips'
@monkey = ActionView::Base.new
# puts @monkey.javascript_include_tag("bar.js", "foo.js", title: "foo")
# puts @mock.stylesheet_link_tag("style.css", "theme.css", media: "all")
# exit
puts "\n\n"
ActionView::Resolver.caching = false
@monkey.javascript_include_tag("bar.js")
@monkey.stylesheet_link_tag("bar.css")
t1 = Time.now
@monkey.javascript_include_tag("bar.js")
@monkey.stylesheet_link_tag("bar.css")
puts "without cache = #{(Time.now - t1) * 1000}"
ActionView::Resolver.caching = true
# warmup
@monkey.javascript_include_tag("bar.js")
@monkey.stylesheet_link_tag("bar.css")
t2 = Time.now
@monkey.javascript_include_tag("bar.js")
@monkey.stylesheet_link_tag("bar.css")
puts "cache = #{(Time.now - t2) * 1000}"
# exit
Benchmark.ips do |x|
x.report("javascript_include_tag witout cache") {
ActionView::Resolver.caching = false
@monkey.javascript_include_tag("bar.js")
}
x.report("javascript_include_tag with cache") {
ActionView::Resolver.caching = true
@monkey.javascript_include_tag("bar.js")
}
x.compare!
end
Benchmark.ips do |x|
x.report("stylesheet_link_tag witout cache") {
ActionView::Resolver.caching = false
@monkey.stylesheet_link_tag("bar.css")
}
x.report("stylesheet_link_tag with cache") {
ActionView::Resolver.caching = true
@monkey.stylesheet_link_tag("bar.css")
}
x.compare!
end
end
@huacnlee
Copy link
Author

without cache = 2.104
cache = 0.06899999999999999
Calculating -------------------------------------
javascript_include_tag witout cache
                       261.000  i/100ms
javascript_include_tag with cache
                         1.254k i/100ms
-------------------------------------------------
javascript_include_tag witout cache
                          3.202k (± 6.1%) i/s -     16.182k
javascript_include_tag with cache
                         49.110k (±10.1%) i/s -    242.022k

Comparison:
javascript_include_tag with cache:    49109.6 i/s
javascript_include_tag witout cache:     3202.2 i/s - 15.34x slower

Calculating -------------------------------------
stylesheet_link_tag witout cache
                       264.000  i/100ms
stylesheet_link_tag with cache
                         1.269k i/100ms
-------------------------------------------------
stylesheet_link_tag witout cache
                          3.159k (± 7.9%) i/s -     15.840k
stylesheet_link_tag with cache
                         47.309k (±11.1%) i/s -    233.496k

Comparison:
stylesheet_link_tag with cache:    47308.8 i/s
stylesheet_link_tag witout cache:     3159.1 i/s - 14.98x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment