Guest User

Untitled

a guest
Jun 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. require 'benchmark'
  2. require 'slim'
  3. require 'erubis'
  4.  
  5. #gem 'haml', '3.0.21'
  6. gem 'haml', '3.1.0.alpha.14'
  7. require 'haml'
  8.  
  9. slim = <<-SLIM
  10. ! doctype html
  11. html
  12. head
  13. title Test
  14. body
  15. h1 simple markup
  16. div#content
  17.  
  18. - unless items.empty?
  19. ol
  20. - for item in items do
  21. li
  22. = item
  23. - else
  24. p No items
  25. SLIM
  26.  
  27. erb = <<-ERB
  28. <!doctype html>
  29. <html>
  30. <head>
  31. <title>Test</title>
  32. </head>
  33. <body>
  34. <h1>simple markup</h1>
  35. <div id="content">
  36. <% unless items.empty? %>
  37. <% for item in items do %>
  38. <li><%= item %></li>
  39. <% end %>
  40. <% else %>
  41. <p>No items</p>
  42. <% end %>
  43. </div>
  44. </body>
  45. </html>
  46. ERB
  47.  
  48. haml= <<-HAML
  49. !!! html
  50. %html
  51. %head
  52. %title Test
  53. %body
  54. %h1 simple markup
  55. %div#content
  56. - unless items.empty?
  57. - for item in items do
  58. %li= item
  59. - else
  60. %p No items
  61. HAML
  62.  
  63. class Foo
  64. attr_reader :items
  65. def initialize
  66. @items = [1, 2, 3, 4, 5]
  67. end
  68. end
  69.  
  70. foo = Foo.new
  71. n = 10_000
  72.  
  73. Benchmark.bmbm do |bench|
  74. bench.report('empty loop') { n.times { } }
  75.  
  76. # compute template every time
  77. bench.report('erubis') { n.times { Erubis::Eruby.new(erb).result(:items => foo.items) } }
  78. bench.report('slim') { n.times { Slim::Engine.new(slim).render(foo) } }
  79. bench.report("haml #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5).render(foo) } }
  80. bench.report("haml (ugly) #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5, :ugly => true).render(foo) } }
  81.  
  82. # cache compiled template
  83. bench.report('erubis (cache)') { t = Erubis::Eruby.new(erb); n.times { t.result(:items => foo.items) } }
  84. bench.report('slim (cache)') { t = Slim::Engine.new(slim); for i in 0..n; t.render(foo); end }
  85. bench.report("haml #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5); n.times { t.render(foo) } }
  86. bench.report("haml (ugly) #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5, :ugly => true); n.times { t.render(foo) } }
  87. end
Add Comment
Please, Sign In to add comment