Guest User

Untitled

a guest
Jun 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1.  
  2. require 'benchmark'
  3. require 'erb'
  4. require 'erubis'
  5.  
  6. #gem 'haml', '3.0.21'
  7. gem 'haml', '3.1.0.alpha.14'
  8. require 'haml'
  9.  
  10. # 0.6.0 beta automatically escapes html
  11. gem 'slim', '~> 0.6.0.beta.3'
  12. require 'slim'
  13.  
  14. erb = <<-ERB
  15. <!doctype html>
  16. <html>
  17. <head>
  18. <title>Test</title>
  19. </head>
  20. <body>
  21. <h1>simple markup</h1>
  22. <div id="content">
  23. <% unless items.empty? %>
  24. <% items.each do |item| %>
  25. <li><%= item %></li>
  26. <% end %>
  27. <% else %>
  28. <p>No items</p>
  29. <% end %>
  30. </div>
  31. <%= tags %>
  32. </body>
  33. </html>
  34. ERB
  35.  
  36. haml= <<-HAML
  37. !!! html
  38. %html
  39. %head
  40. %title Test
  41. %body
  42. %h1 simple markup
  43. #content
  44. - unless items.empty?
  45. - items.each do |item|
  46. %li= item
  47. - else
  48. %p No items
  49. = tags
  50. HAML
  51.  
  52. slim = <<-SLIM
  53. ! doctype html
  54. html
  55. head
  56. title Test
  57. body
  58. h1 simple markup
  59. div#content
  60.  
  61. - unless items.empty?
  62. ol
  63. - items.each do |item|
  64. li
  65. = item
  66. - else
  67. p No items
  68. = tags
  69. SLIM
  70.  
  71. class Context
  72. def items
  73. @items ||= [1, 2, 3, 4, 5]
  74. end
  75.  
  76. def tags
  77. @tags ||= "<p>Something<span>here</span></p>"
  78. end
  79. end
  80.  
  81. cxt = Context.new
  82.  
  83. # Ruby's ERB doesn't like Hash parameters like Erubis
  84. erb_cxt = cxt.instance_eval { binding }
  85.  
  86. n = 10_000
  87.  
  88. Benchmark.bmbm do |bench|
  89. bench.report('emtpy loop') { n.times { } }
  90.  
  91. # compute template every time
  92. bench.report('erb') { n.times { ERB.new(erb).result(erb_cxt) } }
  93. bench.report("erubis #{Erubis::VERSION}") { n.times { Erubis::Eruby.new(erb).result(:items => cxt.items, :tags => cxt.tags) } }
  94. bench.report("fast erubis #{Erubis::VERSION}") { n.times { Erubis::FastEruby.new(erb).result(:items => cxt.items, :tags => cxt.tags) } }
  95.  
  96. bench.report("haml #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5).render(cxt) } }
  97. bench.report("haml (ugly) #{Haml::VERSION}") { n.times { Haml::Engine.new(haml, :format => :html5, :ugly => true).render(cxt) } }
  98. bench.report('slim') { n.times { Slim::Engine.new(slim).render(cxt) } }
  99.  
  100. # cache compiled template
  101. bench.report('erb (cached)') { t = Erubis::Eruby.new(erb); n.times { t.result(erb_cxt) } }
  102. bench.report("erubis #{Erubis::VERSION} (cached)") { t = Erubis::Eruby.new(erb); n.times { t.result(:items => cxt.items, :tags => cxt.tags) } }
  103. bench.report("fast erubis #{Erubis::VERSION} (cached)") { t = Erubis::FastEruby.new(erb); n.times { t.result(:items => cxt.items, :tags => cxt.tags) } }
  104. bench.report("haml #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5); n.times { t.render(cxt) } }
  105. bench.report("haml (ugly) #{Haml::VERSION} (cached)") { t = Haml::Engine.new(haml, :format => :html5, :ugly => true); n.times { t.render(cxt) } }
  106. bench.report('slim (cached)') { t = Slim::Engine.new(slim); n.times { t.render(cxt) } }
  107. end
Add Comment
Please, Sign In to add comment