Created
August 13, 2015 13:59
-
-
Save pixeltrix/f6d43f011d8c6be927b3 to your computer and use it in GitHub Desktop.
Ruby template engine comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
And I'm also posting the results on my machine.