Skip to content

Instantly share code, notes, and snippets.

@madeindjs
Created November 10, 2018 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeindjs/4ba21bf4bed03bf5dc278ab82707dc28 to your computer and use it in GitHub Desktop.
Save madeindjs/4ba21bf4bed03bf5dc278ab82707dc28 to your computer and use it in GitHub Desktop.
Benchmarking of existing templating libs
require 'erb'
require 'haml'
require 'slim'
require 'benchmark'
class Context
attr_reader :title, :content
def initialize(title, content)
@title = title
@content = content
end
def get_binding
binding
end
end
erb_content = <<-HTML
<html>
<head>
<meta charset="utf-8">
<title><%= @title %></title>
</head>
<body>
<h1><%= @title %></h1>
<p><%= @content %></p>
</body>
</html>
HTML
haml_content = <<-HAML
%html
%head
%meta{charset: 'utf-8'}
%title= @title
%body
%h1= @title
%p= @content
HAML
slim_content = <<-SLIM
html
head
meta charset="utf-8"
title = @title
body
h1 = @title
p = @content
SLIM
context = Context.new 'Hello world', 'Lorem ipsum'
puts Benchmark.measure {
50_000.times do
ERB.new(erb_content).result(context.get_binding)
end
}
#=> 5.971182 0.000000 5.971182 ( 5.974689)
puts Benchmark.measure {
50_000.times do
Haml::Engine.new(haml_content).render(context.get_binding)
end
}
#=> 73.619234 0.019773 73.639007 ( 73.665570)
puts Benchmark.measure {
50_000.times do
Slim::Template.new { slim_content }.render(context)
end
}
# => 138.663156 0.056433 138.719589 (138.883643)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment