Forked from anonymous/01_mustache_liquid_benchmark.rb
Last active
March 27, 2022 23:09
-
-
Save jrunning/4b33c3157768edec50a5 to your computer and use it in GitHub Desktop.
Mustache vs. Liquid templates benchmark (naïve)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a very basic benchmark of Mustache (1.0.2) vs. | |
# Liquid (3.0.6) rendering. It does two sets of benchmarks: One with | |
# "precompiled" templates (i.e. the Mustache::Template or | |
# Liquid::Template object is kept between runs) and one without (a new | |
# Template object is instantiated for each run). This benchmark tests | |
# a basic loop; no other features e.g. partials or conditionals | |
# are tested. | |
require "benchmark/ips" | |
require "mustache" | |
require "liquid" | |
mustache_template = <<END | |
{{#products}} | |
<div class='product_brick'> | |
<div class='container'> | |
<div class='element'> | |
<img src='images/{{image}}' class='product_miniature' /> | |
</div> | |
<div class='element description'> | |
<a href={{url}} class='product_name block bold'> | |
{{external_index}} | |
</a> | |
</div> | |
</div> | |
</div> | |
{{/products}} | |
END | |
liquid_template = <<END | |
{% for product in products %} | |
<div class='product_brick'> | |
<div class='container'> | |
<div class='element'> | |
<img src='images/{{ product.image }}' class='product_miniature' /> | |
</div> | |
<div class='element description'> | |
<a href={{ product.url }} class='product_name block bold'> | |
{{ product.external_index }} | |
</a> | |
</div> | |
</div> | |
</div> | |
{% endfor %} | |
END | |
data = { | |
'products' => 100.times.map do |n| | |
{ 'image' => "foo-#{rand 100}.png", 'url' => "http://bar-#{rand 100}.com", 'external_index' => "FOO #{rand 100}" } | |
end | |
} | |
def render_mustache(template, data) | |
tmpl = Mustache::Template.new(template) | |
ctx = Mustache::Context.new(Mustache.new) | |
ctx.push(data) | |
tmpl.render(ctx) | |
end | |
def render_liquid(template, data) | |
Liquid::Template.parse(template).render(data) | |
end | |
mustache_template_pre = Mustache::Template.new(mustache_template) | |
liquid_template_pre = Liquid::Template.parse(liquid_template) | |
def render_mustache_pre(tmpl, data) | |
ctx = Mustache::Context.new(Mustache.new) | |
ctx.push(data) | |
tmpl.render(ctx) | |
end | |
def render_liquid_pre(tmpl, data) | |
tmpl.render(data) | |
end | |
Benchmark.ips do |x| | |
x.report('render_mustache') { render_mustache(mustache_template, data) } | |
x.report('render_mustache_pre') { render_mustache_pre(mustache_template_pre, data) } | |
x.report('render_liquid') { render_liquid(liquid_template, data) } | |
x.report('render_liquid_pre') { render_liquid_pre(liquid_template_pre, data) } | |
x.compare! | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Warming up -------------------------------------- | |
render_mustache 33.000 i/100ms | |
render_mustache_pre 49.000 i/100ms | |
render_liquid 24.000 i/100ms | |
render_liquid_pre 25.000 i/100ms | |
Calculating ------------------------------------- | |
render_mustache 330.242 (± 7.3%) i/s - 1.650k | |
render_mustache_pre 505.154 (± 6.1%) i/s - 2.548k | |
render_liquid 240.000 (± 7.5%) i/s - 1.200k | |
render_liquid_pre 248.057 (± 6.0%) i/s - 1.250k | |
Comparison: | |
render_mustache_pre: 505.2 i/s | |
render_mustache: 330.2 i/s - 1.53x slower | |
render_liquid_pre: 248.1 i/s - 2.04x slower | |
render_liquid: 240.0 i/s - 2.10x slower | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this Liquid-C is much more faster:
New benchmark: