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 | |
Running this Liquid-C is much more faster:
# add these line
require 'liquid'
require 'liquid/c'
New benchmark:
# mustache (1.1.1)
# liquid (5.1.0)
# liquid-c (4.1.0)
Warming up --------------------------------------
render_mustache 95.000 i/100ms
render_mustache_pre 155.000 i/100ms
render_liquid 368.000 i/100ms
render_liquid_pre 410.000 i/100ms
Calculating -------------------------------------
render_mustache 948.676 (± 0.5%) i/s - 4.750k in 5.007094s
render_mustache_pre 1.554k (± 0.4%) i/s - 7.905k in 5.087068s
render_liquid 3.695k (± 1.3%) i/s - 18.768k in 5.080867s
render_liquid_pre 4.150k (± 1.3%) i/s - 20.910k in 5.039370s
Comparison:
render_liquid_pre: 4150.1 i/s
render_liquid: 3694.6 i/s - 1.12x (± 0.00) slower
render_mustache_pre: 1554.0 i/s - 2.67x (± 0.00) slower
render_mustache: 948.7 i/s - 4.37x (± 0.00) slower
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome!
Saved lot of my time.