Guest User

Untitled

a guest
Mar 13th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. require 'benchmark'
  2. require 'examples/complex_view'
  3.  
  4. class Handlebar < ComplexView
  5. def render_sections(template)
  6. # fail fast
  7. return template unless template.include?('{{#')
  8.  
  9. template.gsub(/\{\{\#([^\}]*)\}\}\s*(.+)\{\{\/\1\}\}\s*/m) do |s|
  10. ret = find($1)
  11.  
  12. if ret.respond_to? :each
  13. ret.map do |ctx|
  14. render($2, ctx)
  15. end.join
  16. elsif ret
  17. render($2)
  18. else
  19. ''
  20. end
  21. end
  22. end
  23. end
  24.  
  25. n = 10000
  26. Benchmark.bm(9) do |x|
  27. x.report('complex') { n.times { ComplexView.to_html } }
  28. x.report('handlebar') { n.times { Handlebar.to_html } }
  29. end
  30.  
  31. __END__
  32. user system total real
  33. complex 18.760000 0.710000 19.470000 ( 26.253526)
  34. handlebar 6.320000 0.520000 6.840000 ( 12.494350)
Add Comment
Please, Sign In to add comment