Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created October 22, 2010 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evanphx/639646 to your computer and use it in GitHub Desktop.
Save evanphx/639646 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'erb'
require 'erubis'
require 'bundler'
#gem 'haml', '3.0.21'
# gem 'haml', '3.1.0.alpha.14'
require 'haml'
# 0.6.0 automatically escapes html, using == instead.
gem 'slim', '~> 0.6.1'
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
e1 = ERB.new(erb)
e2 = Erubis::Eruby.new(erb)
e3 = Erubis::FastEruby.new(erb)
h1 = Haml::Engine.new(haml, :format => :html5)
h2 = Haml::Engine.new(haml, :format => :html5, :ugly => true)
s1 = Slim::Engine.new(slim)
ec = Rubinius::Compiler.eval_cache
h = { :items => cxt.items, :tags => cxt.tags }
Benchmark.bmbm do |bench|
# bench.report('emtpy loop') { n.times { } }
bench.report('erb') { n.times { e1.result(erb_cxt) } }
bench.report("erubis #{Erubis::VERSION} (hash)") {
n.times { e2.result(h) }
}
bench.report("erubis #{Erubis::VERSION} (binding)") {
n.times { e2.result(erb_cxt) }
}
bench.report("fast erubis #{Erubis::VERSION} (hash)") {
n.times { e3.result(:items => cxt.items, :tags => cxt.tags) }
}
bench.report("fast erubis #{Erubis::VERSION} (binding)") {
n.times { e3.result(erb_cxt) }
}
bench.report("haml #{Haml::VERSION}") {
n.times { h1.render(cxt) }
}
bench.report("haml (ugly) #{Haml::VERSION}") {
n.times { h2.render(cxt) }
}
bench.report('slim') { n.times { s1.render(cxt) } }
end
p ec.current
p :fin => ec.misses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment