Skip to content

Instantly share code, notes, and snippets.

@kitallis
Last active October 10, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitallis/3746682 to your computer and use it in GitHub Desktop.
Save kitallis/3746682 to your computer and use it in GitHub Desktop.
benchmarks for .} compilation engines, re-using a spec from temple-mustache
require 'bacon'
require 'temple'
require 'temple/mustache'
require 'benchmark'
require 'musterb'
require 'mustache'
require 'erubis'
class Bacon::Context
def random_product
{
:id => rand(10000),
:favorite_id => rand(10000),
:quick_view_url => "http://foo.bar",
:name => "Product name",
:url => "http://foo.url",
:thumbnail => "http://foo.image",
:sku => "sku",
:fabrics => "fabrics",
:price => "price",
:themes => "themes",
:designer => "designer",
:favorited => "favorited"
}
end
def products
16.times.map { random_product }
end
end
describe Temple::Mustache::Template do
it 'should benchmark these three template compilation engines' do
src = %q{
{{#products}}
<div class="product-item" data-variant-id='{{id}}' data-favorite-id='{{favorite_id}}' data-quick-view-url="{{quick_view_url}}">
<h3>Product: {{name}}</h3>
<a href="{{url}}" class="quickView">
<img src="{{thumbnail}}"/>
</a>
<ul>
<li>SKU: {{sku}}</li>
<li>Fabrics: {{fabrics}}</li>
<li>Themes: {{themes}}</li>
<li>Price: {{price}}</li>
<li>Designer: {{designer}}
<li class="favorite">
<div class="not-favorited {{favorited}}">
<button class="add-to-favorites">Add to Favorite</button>
</div>
<div class="favorited {{favorited}}">
<button class="remove-from-favorites">Remove from Favorite</button>
</div>
</li>
</ul>
</div>
{{/products}}
}
mustache = lambda { Mustache.render(src, :products => products) }
compiled_mustache = Temple::Mustache::Template.new { src }
temple_mustache = lambda { compiled_mustache.render(:products => products) }
erb = Musterb.to_erb(src)
compiled_erb = Erubis::Eruby.new(erb)
musterb = lambda { compiled_erb.result(:products => products) }
Benchmark.bmbm do |x|
x.report("mustache - regular") { 1000.times { mustache.call } }
x.report("temple_mustache - pre-parsed") { 1000.times { temple_mustache.call } }
x.report("musterb - pre-parsed") { 1000.times { musterb.call } }
end
puts "\n" * 10
5.should.equal 5
end
it 'should benchmark these three template compilation engines' do
src = %q{
{{#products}}
<div class="product-item" data-variant-id='{{id}}' data-favorite-id='{{favorite_id}}' data-quick-view-url="{{quick_view_url}}">
<h3>Product: {{name}}</h3>
<a href="{{url}}" class="quickView">
<img src="{{thumbnail}}"/>
</a>
<ul>
<li>SKU: {{sku}}</li>
<li>Fabrics: {{fabrics}}</li>
<li>Themes: {{themes}}</li>
<li>Price: {{price}}</li>
<li>Designer: {{designer}}
<li class="favorite">
<div class="not-favorited {{favorited}}">
<button class="add-to-favorites">Add to Favorite</button>
</div>
<div class="favorited {{favorited}}">
<button class="remove-from-favorites">Remove from Favorite</button>
</div>
</li>
</ul>
</div>
{{/products}}
}
mustache = lambda { Mustache.render(src, :products => products) }
temple_mustache = lambda {
compiled_mustache = Temple::Mustache::Template.new { src }.render(:products => products)
}
musterb = lambda {
erb = Musterb.to_erb(src)
compiled_erb = Erubis::Eruby.new(erb)
compiled_erb.result(:products => products)
}
Benchmark.bmbm do |x|
x.report("mustache - regular") { 1000.times { mustache.call } }
x.report("temple_mustache - parse & compile") { 1000.times { temple_mustache.call } }
x.report("musterb - parse & compile") { 1000.times { musterb.call } }
end
puts "\n" * 10
5.should.equal 5
end
it 'should benchmark these three template compilation engines' do
src = %q{
{{#products}}
<div class="product-item" data-variant-id='{{id}}' data-favorite-id='{{favorite_id}}' data-quick-view-url="{{quick_view_url}}">
<h3>Product: {{name}}</h3>
<a href="{{url}}" class="quickView">
<img src="{{thumbnail}}"/>
</a>
<ul>
<li>SKU: {{sku}}</li>
<li>Fabrics: {{fabrics}}</li>
<li>Themes: {{themes}}</li>
<li>Price: {{price}}</li>
<li>Designer: {{designer}}
<li class="favorite">
<div class="not-favorited {{favorited}}">
<button class="add-to-favorites">Add to Favorite</button>
</div>
<div class="favorited {{favorited}}">
<button class="remove-from-favorites">Remove from Favorite</button>
</div>
</li>
</ul>
</div>
{{/products}}
}
mustache = lambda { Mustache.render(src, :products => products) }
temple_mustache = lambda {
compiled_mustache = Temple::Mustache::Template.new { src }.render(:products => products)
}
musterb = lambda {
erb = Musterb.to_erb(src)
compiled_erb = Erubis::Eruby.new(erb)
compiled_erb.evaluate(:products => products)
}
Benchmark.bmbm do |x|
x.report("mustache - regular") { 1000.times { mustache.call } }
x.report("temple_mustache - parse & compile") { 1000.times { temple_mustache.call } }
x.report("musterb - parse & compile & evaluate") { 1000.times { musterb.call } }
end
puts "\n" * 10
5.should.equal 5
end
it 'should benchmark these three template compilation engines' do
src = %q{
{{#products}}
<div class="product-item" data-variant-id='{{id}}' data-favorite-id='{{favorite_id}}' data-quick-view-url="{{quick_view_url}}">
<h3>Product: {{name}}</h3>
<a href="{{url}}" class="quickView">
<img src="{{thumbnail}}"/>
</a>
<ul>
<li>SKU: {{sku}}</li>
<li>Fabrics: {{fabrics}}</li>
<li>Themes: {{themes}}</li>
<li>Price: {{price}}</li>
<li>Designer: {{designer}}
<li class="favorite">
<div class="not-favorited {{favorited}}">
<button class="add-to-favorites">Add to Favorite</button>
</div>
<div class="favorited {{favorited}}">
<button class="remove-from-favorites">Remove from Favorite</button>
</div>
</li>
</ul>
</div>
{{/products}}
}
mustache = lambda { Mustache.render(src, :products => products) }
temple_mustache = lambda {
compiled_mustache = Temple::Mustache::Template.new { src }.render(:products => products)
}
erb = Musterb.to_erb(src)
compiled_erb = Erubis::Eruby.new(erb)
musterb = lambda { compiled_erb.evaluate(:products => products) }
Benchmark.bmbm do |x|
x.report("mustache - regular") { 1000.times { mustache.call } }
x.report("temple_mustache - parse & compile") { 1000.times { temple_mustache.call } }
x.report("musterb - pre-parse & compile & evaluate") { 1000.times { musterb.call } }
end
5.should.equal 5
end
end
@kitallis
Copy link
Author

± rake test
(in /home/c42/code/temple-mustache)
bacon -Ilib -Itest --automatic --quiet
Rehearsal -----------------------------------------------------------
mustache - regular        2.280000   0.000000   2.280000 (  2.280102)
temple_mustache - pre-parsed   0.450000   0.000000   0.450000 (  0.453557)
musterb - pre-parsed      1.070000   0.000000   1.070000 (  1.070074)
-------------------------------------------------- total: 3.800000sec

                              user     system      total        real
mustache - regular        2.300000   0.000000   2.300000 (  2.304680)
temple_mustache - pre-parsed   0.460000   0.000000   0.460000 (  0.453609)
musterb - pre-parsed      1.060000   0.000000   1.060000 (  1.063013)










.Rehearsal ----------------------------------------------------------------
mustache - regular             2.310000   0.000000   2.310000 (  2.306812)
temple_mustache - parse & compile   1.690000   0.000000   1.690000 (  1.689991)
musterb - parse & compile      1.360000   0.000000   1.360000 (  1.369311)
------------------------------------------------------- total: 5.360000sec

                                   user     system      total        real
mustache - regular             2.300000   0.000000   2.300000 (  2.303242)
temple_mustache - parse & compile   1.690000   0.000000   1.690000 (  1.693878)
musterb - parse & compile      1.370000   0.000000   1.370000 (  1.365488)










.Rehearsal ------------------------------------------------------------------------
mustache - regular                     2.300000   0.000000   2.300000 (  2.301501)
temple_mustache - parse & compile           1.670000   0.000000   1.670000 (  1.680713)
musterb - parse & compile & evaluate   0.580000   0.000000   0.580000 (  0.579956)
--------------------------------------------------------------- total: 4.550000sec

                                           user     system      total        real
mustache - regular                     2.280000   0.010000   2.290000 (  2.292036)
temple_mustache - parse & compile           1.680000   0.000000   1.680000 (  1.681803)
musterb - parse & compile & evaluate   0.580000   0.000000   0.580000 (  0.575770)










.Rehearsal ----------------------------------------------------------------------------
mustache - regular                         2.310000   0.000000   2.310000 (  2.310506)
temple_mustache - parse & compile               1.680000   0.000000   1.680000 (  1.688257)
musterb - pre-parse & compile & evaluate   0.120000   0.000000   0.120000 (  0.115015)
------------------------------------------------------------------- total: 4.110000sec

                                               user     system      total        real
mustache - regular                         2.300000   0.000000   2.300000 (  2.303653)
temple_mustache - parse & compile               1.680000   0.000000   1.680000 (  1.690522)
musterb - pre-parse & compile & evaluate   0.110000   0.000000   0.110000 (  0.108374)
.

4 tests, 4 assertions, 0 failures, 0 errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment