Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- require 'erb'
- require 'erubis'
- #gem 'haml', '3.0.21'
- gem 'haml', '3.1.0.alpha.14'
- require 'haml'
- # 0.6.0 beta automatically escapes html
- gem 'slim', '~> 0.6.0.beta.3'
- require 'slim'
- erb = <<-ERB
- <!doctype html>
- <html>
- <head>
- <title>Test</title>
- </head>
- <body>
- <h1>simple markup</h1>
- <div id="content">
- <% unless items.empty? %>
- <% items.each do |item| %>
- <li><%= item %></li>
- <% end %>
- <% else %>
- <p>No items</p>
- <% end %>
- </div>
- <%= tags %>
- </body>
- </html>
- ERB
- haml= <<-HAML
- !!! html
- %html
- %head
- %title Test
- %body
- %h1 simple markup
- #content
- - unless items.empty?
- - items.each do |item|
- %li= item
- - else
- %p No items
- = tags
- HAML
- slim = <<-SLIM
- ! doctype html
- html
- head
- title Test
- body
- h1 simple markup
- div#content
- - unless items.empty?
- ol
- - items.each do |item|
- li
- = item
- - else
- p No items
- = tags
- SLIM
- class Context
- def items
- @items ||= [1, 2, 3, 4, 5]
- end
- def tags
- @tags ||= "<p>Something<span>here</span></p>"
- end
- end
- cxt = Context.new
- # Ruby's ERB doesn't like Hash parameters like Erubis
- erb_cxt = cxt.instance_eval { binding }
- n = 10_000
- Benchmark.bmbm do |bench|
- bench.report('emtpy loop') { n.times { } }
- # compute template every time
- bench.report('erb') { n.times { ERB.new(erb).result(erb_cxt) } }
- bench.report("erubis #{Erubis::VERSION}") { n.times { Erubis::Eruby.new(erb).result(:items => cxt.items, :tags => cxt.tags) } }
- bench.report("fast erubis #{Erubis::VERSION}") { n.times { Erubis::FastEruby.new(erb).result(:items => cxt.items, :tags => cxt.tags) } }
- bench.report("haml #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5).render(cxt) } }
- bench.report("haml (ugly) #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5, :ugly => true).render(cxt) } }
- bench.report('slim') { n.times { Slim::Engine.new(slim).render(cxt) } }
- # cache compiled template
- bench.report('erb (cached)') { t = Erubis::Eruby.new(erb); n.times { t.result(erb_cxt) } }
- bench.report("erubis #{Erubis::VERSION} (cached)") { t = Erubis::Eruby.new(erb); n.times { t.result(:items => cxt.items, :tags => cxt.tags) } }
- bench.report("fast erubis #{Erubis::VERSION} (cached)") { t = Erubis::FastEruby.new(erb); n.times { t.result(:items => cxt.items, :tags => cxt.tags) } }
- bench.report("haml #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5); n.times { t.render(cxt) } }
- bench.report("haml (ugly) #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5, :ugly => true); n.times { t.render(cxt) } }
- bench.report('slim (cached)') { t = Slim::Engine.new(slim); n.times { t.render(cxt) } }
- end
Add Comment
Please, Sign In to add comment