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 %> 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| %>
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 %> 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 @@ + + + + Customers List + + + {% 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/liquid/small.liquid b/templates/liquid/small.liquid new file mode 100644 index 0000000..e2dc06e --- /dev/null +++ b/templates/liquid/small.liquid @@ -0,0 +1,18 @@ + + + + {{ title }} + + +

Hello, {{ username }}!

+

+ 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. +

+ + diff --git a/templates/slim/big.slim b/templates/slim/big.slim new file mode 100644 index 0000000..a92f32c --- /dev/null +++ b/templates/slim/big.slim @@ -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'] diff --git a/templates/slim/small.slim b/templates/slim/small.slim new file mode 100644 index 0000000..8b785f1 --- /dev/null +++ b/templates/slim/small.slim @@ -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.