1
0
Fork 0

Slim, Liquid templates

This commit is contained in:
Gregory Eremin 2013-12-14 02:40:54 +07:00
parent b41d194a63
commit 41d073b785
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
12 changed files with 171 additions and 40 deletions

View File

@ -5,3 +5,5 @@ gem 'tilt'
gem 'erubis'
gem 'haml'
gem 'slim'
gem 'liquid'

View File

@ -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

View File

@ -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)
```

View File

@ -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

View File

@ -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

View File

@ -7,18 +7,18 @@
<% customers.each do |customer| %>
<dl>
<dt>Name:</dt>
<dd><%= customer[:name] %></dd>
<dd><%= customer['name'] %></dd>
<dt>Age:</dt>
<dd><%= customer[:age] %></dd>
<dd><%= customer['age'] %></dd>
<dt>Address:</dt>
<dd>
<%= customer[:zip] %>
<%= customer[:country] %>
<%= customer[:city] %>
<%= customer[:address] %>
<%= customer['zip'] %>
<%= customer['country'] %>
<%= customer['city'] %>
<%= customer['address'] %>
</dd>
<dt>Employer:</dt>
<dd><%= customer[:employer] %></dd>
<dd><%= customer['employer'] %></dd>
</dl>
<% end %>
</body>

View File

@ -7,18 +7,18 @@
<% customers.each do |customer| %>
<dl>
<dt>Name:</dt>
<dd><%= customer[:name] %></dd>
<dd><%= customer['name'] %></dd>
<dt>Age:</dt>
<dd><%= customer[:age] %></dd>
<dd><%= customer['age'] %></dd>
<dt>Address:</dt>
<dd>
<%= customer[:zip] %>
<%= customer[:country] %>
<%= customer[:city] %>
<%= customer[:address] %>
<%= customer['zip'] %>
<%= customer['country'] %>
<%= customer['city'] %>
<%= customer['address'] %>
</dd>
<dt>Employer:</dt>
<dd><%= customer[:employer] %></dd>
<dd><%= customer['employer'] %></dd>
</dl>
<% end %>
</body>

View File

@ -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']

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Customers List</title>
</head>
<body>
{% for customer in customers %}
<dl>
<dt>Name:</dt>
<dd>{{ customer.name }}</dd>
<dt>Age:</dt>
<dd>{{ customer.age }}</dd>
<dt>Address:</dt>
<dd>
{{ customer.zip }}
{{ customer.country }}
{{ customer.city }}
{{ customer.address }}
</dd>
<dt>Employer:</dt>
<dd>{{ customer.employer }}</dd>
</dl>
{% endfor %}
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ username }}!</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
</body>
</html>

19
templates/slim/big.slim Normal file
View File

@ -0,0 +1,19 @@
doctype html
html
head
title Customers List
body
- customers.each do |customer|
dl
dt Name:
dd = customer['name']
dt Age:
dd = customer['age']
dt Address:
dd
== customer['zip']
== customer['country']
== customer['city']
== customer['address']
dt Employer:
dd = customer['employer']

14
templates/slim/small.slim Normal file
View File

@ -0,0 +1,14 @@
doctype html
html
head
title = title
body
h1 Hello, #{username}!
p
| Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
| tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
| veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
| commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
| velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
| occaecat cupidatat non proident, sunt in culpa qui officia deserunt
| mollit anim id est laborum.