Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created August 13, 2015 13:59
Show Gist options
  • Save pixeltrix/f6d43f011d8c6be927b3 to your computer and use it in GitHub Desktop.
Save pixeltrix/f6d43f011d8c6be927b3 to your computer and use it in GitHub Desktop.
Ruby template engine comparison
$ ruby template_engine_shootout.rb
Fetching gem metadata from https://rubygems.org/....
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Using benchmark-ips 2.3.0
Using erubis 2.7.0
Using tilt 2.0.1
Using haml 4.0.7
Using temple 0.7.6
Using slim 3.0.6
Using bundler 1.10.6
Calculating -------------------------------------
ERB 13.766k i/100ms
Erubis 17.945k i/100ms
Haml 6.672k i/100ms
Slim 15.101k i/100ms
-------------------------------------------------
ERB 157.989k (± 6.8%) i/s - 798.428k
Erubis 205.408k (± 7.7%) i/s - 1.023M
Haml 73.924k (± 6.2%) i/s - 373.632k
Slim 170.302k (± 7.4%) i/s - 860.757k
Comparison:
Erubis: 205407.8 i/s
Slim : 170302.0 i/s - 1.21x slower
ERB : 157989.2 i/s - 1.30x slower
Haml : 73924.0 i/s - 2.78x slower
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'benchmark-ips', '2.3.0'
gem 'erubis', '2.7.0'
gem 'haml', '4.0.7'
gem 'slim', '3.0.6'
gem 'tilt', '2.0.1'
end
SCOPE = Object.new
LOCALS = { message: 'Hello, ERB' }
TEMPLATE_ERB = Tilt::ERBTemplate.new do
<<-EOF
<html>
<head>
<title><%= message %></title>
</head>
<body>
<h1><%= message %></h1>
</body>
</html>
EOF
end
# Rails uses Erubis to render erb templates
TEMPLATE_ERUBIS = Tilt::ErubisTemplate.new do
<<-EOF
<html>
<head>
<title><%= message %></title>
</head>
<body>
<h1><%= message %></h1>
</body>
</html>
EOF
end
TEMPLATE_HAML = Tilt::HamlTemplate.new do
<<-EOF
%html
%head
%title= message
%body
%h1= message
EOF
end
TEMPLATE_SLIM = Slim::Template.new do
<<-EOF
html
head
title = message
body
h1 = message
EOF
end
Benchmark.ips do |x|
x.report('ERB ') do
TEMPLATE_ERB.render(SCOPE, LOCALS)
end
x.report('Erubis') do
TEMPLATE_ERUBIS.render(SCOPE, LOCALS)
end
x.report('Haml ') do
TEMPLATE_HAML.render(SCOPE, LOCALS)
end
x.report('Slim ') do
TEMPLATE_SLIM.render(SCOPE, LOCALS)
end
x.compare!
end
@boazsegev
Copy link

boazsegev commented Mar 22, 2019

Thank you for the benchmark code!

I updated the code to benchmark recent versions and added two mustache template engines into the mix.

You can click here to read the updated code.

# original code from: https://gist.github.com/pixeltrix/f6d43f011d8c6be927b3
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
  source 'https://rubygems.org'
  gem 'benchmark-ips', '2.7.2'
  gem 'erubi', '1.8.0'
  gem 'haml', '5.0.4'
  gem 'slim', '4.0.1'
  gem 'tilt', '2.0.9'
  gem 'mustache', '1.1.0'
  gem 'iodine', '0.7.27'
end

# require 'benchmark/ips' # '2.7.2'
# require 'erubi'         # '1.8.0'
# require 'haml'          # '5.0.4'
# require 'slim'          # '4.0.1'
# require 'mustache'      # '1.1.0'
# require 'iodine'        # '0.7.27'
# require 'tilt'          # '2.0.9'



SCOPE  = Object.new
LOCALS = { message: 'Hello, Ruby' }

TEMPLATE_ERB = Tilt::ERBTemplate.new do
<<-EOF
<html>
  <head>
    <title><%= message %></title>
  </head>
  <body>
    <h1><%= message %></h1>
  </body>
</html>
EOF
end
# Rails uses Erubis to render erb templates
TEMPLATE_ERUBI = Tilt::ErubiTemplate.new do
<<-EOF
<html>
  <head>
    <title><%= message %></title>
  </head>
  <body>
    <h1><%= message %></h1>
  </body>
</html>
EOF
end
TEMPLATE_HAML = Tilt::HamlTemplate.new do
<<-EOF
%html
  %head
    %title= message
  %body
    %h1= message
EOF
end
TEMPLATE_SLIM = Slim::Template.new do
<<-EOF
html
  head
    title = message
  body
    h1 = message
EOF
end

TEMPLATE_IODINE = Iodine::Mustache.new nil, <<-EOF
<html>
  <head>
    <title>{{ message }}</title>
  </head>
  <body>
    <h1>{{ message }}</h1>
  </body>
</html>
EOF

TEMPLATE_MUSTACHE_RB = ::Mustache.new
TEMPLATE_MUSTACHE_RB.template = <<-EOF
<html>
  <head>
    <title>{{ message }}</title>
  </head>
  <body>
    <h1>{{ message }}</h1>
  </body>
</html>
EOF
TEMPLATE_MUSTACHE_RB.render(LOCALS)

Benchmark.ips do |x|
  x.report('ERB            ') do
    TEMPLATE_ERB.render(SCOPE, LOCALS)
  end
  x.report('Erubi          ') do
    TEMPLATE_ERUBI.render(SCOPE, LOCALS)
  end
  x.report('Haml           ') do
    TEMPLATE_HAML.render(SCOPE, LOCALS)
  end
  x.report('Slim           ') do
    TEMPLATE_SLIM.render(SCOPE, LOCALS)
  end
  x.report('Mustache (ruby)') do
    TEMPLATE_MUSTACHE_RB.render(LOCALS)
  end
  x.report('Iodine         ') do
    TEMPLATE_IODINE.render(LOCALS)
  end
  x.compare!
end

And I'm also posting the results on my machine.
Warming up --------------------------------------
     ERB                26.542k i/100ms
     Erubi              28.417k i/100ms
     Haml               21.320k i/100ms
     Slim               26.771k i/100ms
     Iodine             87.182k i/100ms
     Mustache (ruby)    28.380k i/100ms
Calculating -------------------------------------
     ERB                289.222k (± 1.4%) i/s -      1.460M in   5.048336s
     Erubi              307.800k (± 2.1%) i/s -      1.563M in   5.080075s
     Haml               239.131k (± 2.1%) i/s -      1.215M in   5.084171s
     Slim               305.695k (± 2.1%) i/s -      1.553M in   5.081571s
     Iodine             976.444k (± 9.8%) i/s -      4.882M in   5.055869s
     Mustache (ruby)    290.639k (± 7.8%) i/s -      1.476M in   5.111807s

Comparison:
     Iodine         :   976444.3 i/s
     Erubi          :   307799.9 i/s - 3.17x  slower
     Slim           :   305694.5 i/s - 3.19x  slower
     Mustache (ruby):   290638.7 i/s - 3.36x  slower
     ERB            :   289222.1 i/s - 3.38x  slower
     Haml           :   239131.1 i/s - 4.08x  slower

@mochadwi
Copy link

Erubi: https://github.com/jeremyevans/erubi

Erubi is different than Erubis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment