From 41d073b78541d2ac77bda58222ee1c8d3ddfd80e Mon Sep 17 00:00:00 2001
From: Gregory Eremin
Date: Sat, 14 Dec 2013 02:40:54 +0700
Subject: [PATCH] Slim, Liquid templates
---
Gemfile | 2 ++
Gemfile.lock | 7 ++++++
README.md | 42 +++++++++++++++++++++++++++--------
benchmark.rb | 28 ++++++++++++++++++++---
fakedata.rb | 14 ++++++------
templates/erb/big.erb | 14 ++++++------
templates/erubis/big.erubis | 14 ++++++------
templates/haml/big.haml | 14 ++++++------
templates/liquid/big.liquid | 25 +++++++++++++++++++++
templates/liquid/small.liquid | 18 +++++++++++++++
templates/slim/big.slim | 19 ++++++++++++++++
templates/slim/small.slim | 14 ++++++++++++
12 files changed, 171 insertions(+), 40 deletions(-)
create mode 100644 templates/liquid/big.liquid
create mode 100644 templates/liquid/small.liquid
create mode 100644 templates/slim/big.slim
create mode 100644 templates/slim/small.slim
diff --git a/Gemfile b/Gemfile
index e12d85b..18f43f4 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,3 +5,5 @@ gem 'tilt'
gem 'erubis'
gem 'haml'
+gem 'slim'
+gem 'liquid'
diff --git a/Gemfile.lock b/Gemfile.lock
index d440696..b4cee34 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -7,6 +7,11 @@ GEM
haml (4.0.4)
tilt
i18n (0.6.9)
+ liquid (2.6.0)
+ slim (2.0.2)
+ temple (~> 0.6.6)
+ tilt (>= 1.3.3, < 2.1)
+ temple (0.6.7)
tilt (2.0.0)
PLATFORMS
@@ -16,4 +21,6 @@ DEPENDENCIES
erubis
faker
haml
+ liquid
+ slim
tilt
diff --git a/README.md b/README.md
index 38262f8..f5abd87 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,7 @@ cd template_engine_comparison
bundle install
# Generate data for templates
-./fakedata.rb 10000 > data/big.yml
+./fakedata.rb 1000 > data/big.yml
# Run benchmarks
./benchmark.rb
@@ -17,18 +17,42 @@ bundle install
## Current Results
```
############################################################
-## Compilation (1000 runs) ##
+## Compilation (small) (1000 runs) ##
############################################################
user system total real
-erb 0.230000 0.020000 0.250000 ( 0.247248)
-erubis 0.050000 0.010000 0.060000 ( 0.061928)
-haml 0.880000 0.050000 0.930000 ( 0.935885)
+erb 0.250000 0.030000 0.280000 ( 0.275828)
+erubis 0.060000 0.020000 0.080000 ( 0.077510)
+haml 0.860000 0.030000 0.890000 ( 0.892039)
+slim 1.960000 0.030000 1.990000 ( 1.989972)
+liquid 0.060000 0.020000 0.080000 ( 0.074439)
############################################################
-## Render (10000 runs) ##
+## Render (small) (1000 runs) ##
############################################################
user system total real
-erb 0.140000 0.000000 0.140000 ( 0.136514)
-erubis 0.080000 0.000000 0.080000 ( 0.085103)
-haml 0.250000 0.020000 0.270000 ( 0.263665)
+erb 0.010000 0.000000 0.010000 ( 0.012331)
+erubis 0.010000 0.000000 0.010000 ( 0.008456)
+haml 0.020000 0.000000 0.020000 ( 0.027852)
+slim 0.030000 0.000000 0.030000 ( 0.021398)
+liquid 0.030000 0.000000 0.030000 ( 0.037635)
+
+############################################################
+## Compilation (big) (1000 runs) ##
+############################################################
+ user system total real
+erb 0.330000 0.020000 0.350000 ( 0.352752)
+erubis 0.120000 0.020000 0.140000 ( 0.139981)
+haml 1.200000 0.030000 1.230000 ( 1.226896)
+slim 2.440000 0.030000 2.470000 ( 2.469225)
+liquid 0.120000 0.020000 0.140000 ( 0.133451)
+
+############################################################
+## Render (big) (100 runs) ##
+############################################################
+ user system total real
+erb 0.650000 0.020000 0.670000 ( 0.667166)
+erubis 0.430000 0.010000 0.440000 ( 0.446348)
+haml 0.970000 0.020000 0.990000 ( 0.993755)
+slim 1.440000 0.010000 1.450000 ( 1.445460)
+liquid 10.660000 0.050000 10.710000 ( 10.715820)
```
diff --git a/benchmark.rb b/benchmark.rb
index 62d8161..045ab81 100755
--- a/benchmark.rb
+++ b/benchmark.rb
@@ -4,12 +4,16 @@ require 'rubygems'
require 'bundler/setup'
require 'benchmark'
require 'yaml'
+require 'ostruct'
require 'erubis'
require 'haml'
+require 'slim'
+require 'liquid'
require 'tilt/erb'
require 'tilt/erubis'
require 'tilt/haml'
+require 'tilt/liquid'
# Terminal settings
TERMINAL_WIDTH = 60
@@ -59,6 +63,14 @@ ENGINES = {
haml: {
class: Tilt::HamlTemplate,
extension: 'haml'
+ },
+ slim: {
+ class: Slim::Template,
+ extension: 'slim'
+ },
+ liquid: {
+ class: Tilt::LiquidTemplate,
+ extension: 'liquid'
}
}
@@ -72,7 +84,12 @@ puts
banner 'Render (small) (%d runs)' % RENDER_LOOPS
benchmark(loops: RENDER_LOOPS) do |name, attrs|
- templates[name].render(Object, small_data)
+ case name
+ when :slim
+ templates[name].render(OpenStruct.new(small_data))
+ else
+ templates[name].render(Object, small_data)
+ end
end
puts
@@ -83,8 +100,13 @@ benchmark(loops: COMPILE_LOOPS) do |name, attrs|
end
puts
-banner 'Render (big) (%d runs)' % RENDER_LOOPS
+banner 'Render (big) (%d runs)' % 100
benchmark(loops: 100) do |name, attrs|
- templates[name].render(Object, customers: big_data)
+ case name
+ when :slim
+ templates[name].render(OpenStruct.new(customers: big_data))
+ else
+ templates[name].render(Object, {customers: big_data})
+ end
end
puts
diff --git a/fakedata.rb b/fakedata.rb
index 4e70b4c..c640ac7 100755
--- a/fakedata.rb
+++ b/fakedata.rb
@@ -13,13 +13,13 @@ end
data = 1.upto(num_records).map do
{
- name: Faker::Name.name,
- age: rand(20..50),
- country: Faker::Address.country,
- city: Faker::Address.city,
- zip: Faker::Address.zip.to_i,
- address: Faker::Address.street_address(true),
- employer: Faker::Company.name
+ 'name' => Faker::Name.name,
+ 'age' => rand(20..50),
+ 'country' => Faker::Address.country,
+ 'city' => Faker::Address.city,
+ 'zip' => Faker::Address.zip.to_i,
+ 'address' => Faker::Address.street_address(true),
+ 'employer' => Faker::Company.name
}
end
diff --git a/templates/erb/big.erb b/templates/erb/big.erb
index 10f3cc5..4295334 100644
--- a/templates/erb/big.erb
+++ b/templates/erb/big.erb
@@ -7,18 +7,18 @@
<% customers.each do |customer| %>
- Name:
- - <%= customer[:name] %>
+ - <%= customer['name'] %>
- Age:
- - <%= customer[:age] %>
+ - <%= customer['age'] %>
- Address:
-
- <%= customer[:zip] %>
- <%= customer[:country] %>
- <%= customer[:city] %>
- <%= customer[:address] %>
+ <%= customer['zip'] %>
+ <%= customer['country'] %>
+ <%= customer['city'] %>
+ <%= customer['address'] %>
- Employer:
- - <%= customer[:employer] %>
+ - <%= customer['employer'] %>
<% end %>
+ {% for customer in customers %}
+
+ - Name:
+ - {{ customer.name }}
+ - Age:
+ - {{ customer.age }}
+ - Address:
+ -
+ {{ customer.zip }}
+ {{ customer.country }}
+ {{ customer.city }}
+ {{ customer.address }}
+
+ - Employer:
+ - {{ customer.employer }}
+
+ {% endfor %}
+
diff --git a/templates/erubis/big.erubis b/templates/erubis/big.erubis
index 10f3cc5..4295334 100644
--- a/templates/erubis/big.erubis
+++ b/templates/erubis/big.erubis
@@ -7,18 +7,18 @@
<% customers.each do |customer| %>
% end %>
diff --git a/templates/haml/big.haml b/templates/haml/big.haml
index 0a661e3..dc9a768 100644
--- a/templates/haml/big.haml
+++ b/templates/haml/big.haml
@@ -6,14 +6,14 @@
- customers.each do |customer|
%dl
%dt Name:
- %dd= customer[:name]
+ %dd= customer['name']
%dt Age:
- %dd= customer[:age]
+ %dd= customer['age']
%dt Address:
%dd
- = customer[:zip]
- = customer[:country]
- = customer[:city]
- = customer[:address]
+ = customer['zip']
+ = customer['country']
+ = customer['city']
+ = customer['address']
%dt Employer:
- %dd= customer[:employer]
+ %dd= customer['employer']
diff --git a/templates/liquid/big.liquid b/templates/liquid/big.liquid
new file mode 100644
index 0000000..f28f8c0
--- /dev/null
+++ b/templates/liquid/big.liquid
@@ -0,0 +1,25 @@
+
+
+