1
0
Fork 0

Add Mustache

This commit is contained in:
Gregory Eremin 2013-12-14 03:29:37 +07:00
parent babdc13c79
commit 0425c8bac9
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
6 changed files with 113 additions and 32 deletions

View File

@ -7,3 +7,4 @@ gem 'erubis'
gem 'haml' gem 'haml'
gem 'slim' gem 'slim'
gem 'liquid' gem 'liquid'
gem 'mustache'

View File

@ -8,6 +8,7 @@ GEM
tilt tilt
i18n (0.6.9) i18n (0.6.9)
liquid (2.6.0) liquid (2.6.0)
mustache (0.99.5)
slim (2.0.2) slim (2.0.2)
temple (~> 0.6.6) temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1) tilt (>= 1.3.3, < 2.1)
@ -22,5 +23,6 @@ DEPENDENCIES
faker faker
haml haml
liquid liquid
mustache
slim slim
tilt tilt

View File

@ -1,4 +1,15 @@
# Template Engine Comparison # Template Engine Performance Comparison
#### Ruby implementations.
## Competitors
- ERB
- Erubis
- Haml
- Liquid
- Mustache
- Slim
... and our special guest ...
- String interpolation
## Running ## Running
```bash ```bash
@ -22,43 +33,47 @@ bundle install
## Compilation (small) (1000 runs) ## ## Compilation (small) (1000 runs) ##
############################################################ ############################################################
user system total real user system total real
string 0.040000 0.010000 0.050000 ( 0.054310) string 0.040000 0.020000 0.060000 ( 0.053855)
erb 0.240000 0.030000 0.270000 ( 0.260111) erubis 0.060000 0.010000 0.070000 ( 0.075751)
erubis 0.060000 0.010000 0.070000 ( 0.078122) erb 0.240000 0.030000 0.270000 ( 0.262077)
haml 0.850000 0.030000 0.880000 ( 0.873031) haml 0.840000 0.020000 0.860000 ( 0.865688)
slim 1.930000 0.030000 1.960000 ( 1.960224) slim 1.930000 0.040000 1.970000 ( 1.968993)
liquid 0.050000 0.020000 0.070000 ( 0.073867) mustache 0.020000 0.010000 0.030000 ( 0.030289)
liquid 0.080000 0.020000 0.100000 ( 0.104174)
############################################################ ############################################################
## Render (small) (1000 runs) ## ## Render (small) (1000 runs) ##
############################################################ ############################################################
user system total real user system total real
string 0.010000 0.000000 0.010000 ( 0.013322) string 0.010000 0.000000 0.010000 ( 0.007824)
erb 0.010000 0.000000 0.010000 ( 0.012137) erubis 0.010000 0.000000 0.010000 ( 0.008019)
erubis 0.010000 0.000000 0.010000 ( 0.008176) erb 0.010000 0.000000 0.010000 ( 0.010448)
haml 0.020000 0.000000 0.020000 ( 0.021298) haml 0.020000 0.000000 0.020000 ( 0.019507)
slim 0.020000 0.000000 0.020000 ( 0.022210) slim 0.020000 0.000000 0.020000 ( 0.019644)
liquid 0.030000 0.000000 0.030000 ( 0.036494) mustache 0.230000 0.000000 0.230000 ( 0.232938)
liquid 0.040000 0.000000 0.040000 ( 0.044189)
############################################################ ############################################################
## Compilation (big) (1000 runs) ## ## Compilation (big) (1000 runs) ##
############################################################ ############################################################
user system total real user system total real
string 0.060000 0.020000 0.080000 ( 0.071830) string 0.030000 0.020000 0.050000 ( 0.044706)
erb 0.300000 0.020000 0.320000 ( 0.324309) erubis 0.140000 0.020000 0.160000 ( 0.161244)
erubis 0.120000 0.020000 0.140000 ( 0.140036) erb 0.320000 0.030000 0.350000 ( 0.341144)
haml 1.180000 0.030000 1.210000 ( 1.203810) haml 1.180000 0.020000 1.200000 ( 1.205595)
slim 2.410000 0.030000 2.440000 ( 2.441857) slim 2.410000 0.040000 2.450000 ( 2.449355)
liquid 0.120000 0.010000 0.130000 ( 0.134647) mustache 0.020000 0.010000 0.030000 ( 0.028152)
liquid 0.110000 0.020000 0.130000 ( 0.133382)
############################################################ ############################################################
## Render (big) (100 runs) ## ## Render (big) (100 runs) ##
############################################################ ############################################################
user system total real user system total real
string 0.390000 0.030000 0.420000 ( 0.414645) string 0.390000 0.020000 0.410000 ( 0.408272)
erb 0.650000 0.020000 0.670000 ( 0.666250) erubis 0.440000 0.020000 0.460000 ( 0.457214)
erubis 0.430000 0.010000 0.440000 ( 0.448594) erb 0.640000 0.020000 0.660000 ( 0.656181)
haml 0.970000 0.020000 0.990000 ( 0.981991) haml 0.940000 0.020000 0.960000 ( 0.965373)
slim 1.400000 0.010000 1.410000 ( 1.408893) slim 1.390000 0.000000 1.390000 ( 1.395409)
liquid 10.480000 0.050000 10.530000 ( 10.535895) mustache 3.770000 0.010000 3.780000 ( 3.778392)
liquid 10.460000 0.050000 10.510000 ( 10.507224)
``` ```

View File

@ -10,6 +10,7 @@ require 'erubis'
require 'haml' require 'haml'
require 'slim' require 'slim'
require 'liquid' require 'liquid'
require 'mustache'
require 'tilt/erb' require 'tilt/erb'
require 'tilt/erubis' require 'tilt/erubis'
require 'tilt/haml' require 'tilt/haml'
@ -51,19 +52,35 @@ def benchmark params
end end
end end
module Wrappers
class Base
attr_reader :tpl
def initialize path
@tpl = File.read(path)
end
end
class Mustache < Base
def render context, args = {}
::Mustache.render(tpl, args)
end
end
end
ENGINES = { ENGINES = {
string: { string: {
class: Tilt::StringTemplate, class: Tilt::StringTemplate,
extension: 'str' extension: 'str'
}, },
erb: {
class: Tilt::ERBTemplate,
extension: 'erb'
},
erubis: { erubis: {
class: Tilt::ErubisTemplate, class: Tilt::ErubisTemplate,
extension: 'erubis' extension: 'erubis'
}, },
erb: {
class: Tilt::ERBTemplate,
extension: 'erb'
},
haml: { haml: {
class: Tilt::HamlTemplate, class: Tilt::HamlTemplate,
extension: 'haml' extension: 'haml'
@ -72,12 +89,15 @@ ENGINES = {
class: Slim::Template, class: Slim::Template,
extension: 'slim' extension: 'slim'
}, },
mustache: {
class: Wrappers::Mustache,
extension: 'mustache'
},
liquid: { liquid: {
class: Tilt::LiquidTemplate, class: Tilt::LiquidTemplate,
extension: 'liquid' extension: 'liquid'
},
} }
}
banner 'Compilation (small) (%d runs)' % COMPILE_LOOPS banner 'Compilation (small) (%d runs)' % COMPILE_LOOPS
benchmark(loops: COMPILE_LOOPS) do |name, attrs| benchmark(loops: COMPILE_LOOPS) do |name, attrs|

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Customers List</title>
</head>
<body>
{{# customers }}
<dl>
<dt>Name:</dt>
<dd>{{ name }}</dd>
<dt>Age:</dt>
<dd>{{ age }}</dd>
<dt>Address:</dt>
<dd>
{{ zip }}
{{ country }}
{{ city }}
{{ address }}
</dd>
<dt>Employer:</dt>
<dd>{{ employer }}</dd>
</dl>
{{/ customers }}
</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>