Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- require 'slim'
- require 'erubis'
- #gem 'haml', '3.0.21'
- gem 'haml', '3.1.0.alpha.14'
- require 'haml'
- slim = <<-SLIM
- ! doctype html
- html
- head
- title Test
- body
- h1 simple markup
- div#content
- - unless items.empty?
- ol
- - for item in items do
- li
- = item
- - else
- p No items
- SLIM
- erb = <<-ERB
- <!doctype html>
- <html>
- <head>
- <title>Test</title>
- </head>
- <body>
- <h1>simple markup</h1>
- <div id="content">
- <% unless items.empty? %>
- <% for item in items do %>
- <li><%= item %></li>
- <% end %>
- <% else %>
- <p>No items</p>
- <% end %>
- </div>
- </body>
- </html>
- ERB
- haml= <<-HAML
- !!! html
- %html
- %head
- %title Test
- %body
- %h1 simple markup
- %div#content
- - unless items.empty?
- - for item in items do
- %li= item
- - else
- %p No items
- HAML
- class Foo
- attr_reader :items
- def initialize
- @items = [1, 2, 3, 4, 5]
- end
- end
- foo = Foo.new
- n = 10_000
- Benchmark.bmbm do |bench|
- bench.report('empty loop') { n.times { } }
- # compute template every time
- bench.report('erubis') { n.times { Erubis::Eruby.new(erb).result(:items => foo.items) } }
- bench.report('slim') { n.times { Slim::Engine.new(slim).render(foo) } }
- bench.report("haml #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5).render(foo) } }
- bench.report("haml (ugly) #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5, :ugly => true).render(foo) } }
- # cache compiled template
- bench.report('erubis (cache)') { t = Erubis::Eruby.new(erb); n.times { t.result(:items => foo.items) } }
- bench.report('slim (cache)') { t = Slim::Engine.new(slim); for i in 0..n; t.render(foo); end }
- bench.report("haml #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5); n.times { t.render(foo) } }
- bench.report("haml (ugly) #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5, :ugly => true); n.times { t.render(foo) } }
- end
Add Comment
Please, Sign In to add comment