I decided to continue reviewing the template engines. Our today’s library implements Mustache syntax, which also implemented for many other languages.
Mustache's
syntax is very simple and does not allow to write complex
application logic. You will find some examples in this documentation:
http://mustache.github.io/mustache.5.html
Let’s try to rewrite our performance test from the zenekindarl post to
Mustache
!
POFTHEDAY> (mustache:define render
"
<title>{{title}}</title>
<ul>
{{#items}}<li>{{value}}</li>{{/items}}
</ul>
")
POFTHEDAY> (with-output-to-string (out)
(render '((:title . "Foo Bar")
(:items .
(((:value . "One"))
((:value . "Two"))
((:value . "Three")))))
out))
"
<title>Foo Bar</title>
<ul>
<li>One</li><li>Two</li><li>Three</li>
</ul>
"
POFTHEDAY> (time
(loop with context = '((:title . "Foo Bar")
(:items .
(((:value . "One"))
((:value . "Two"))
((:value . "Three")))))
repeat 1000000
do (with-output-to-string (out)
(render context out))))
Evaluation took:
5.213 seconds of real time
5.252826 seconds of total run time (5.155530 user, 0.097296 system)
[ Run times consist of 0.445 seconds GC time, and 4.808 seconds non-GC time. ]
100.77% CPU
11,510,317,038 processor cycles
4,319,993,136 bytes consed
So, the results are slightly slower than Spinneret
is almost as slow as
Python’s Jinja2
:
<div style='margin-top: 1em;'><span style='width: 600.0px; background-color: red; color: white; padding: 0.5em; display: inline-block;'>Jinja2</span> – <span>6.18 µs</span></div><div style='margin-top: 1em;'><span style='width: 149.51456px; background-color: green; color: white; padding: 0.5em; display: inline-block;'>zenekindarl</span> – <span>1.54 µs</span></div><div style='margin-top: 1em;'><span style='width: 159.2233px; background-color: blue; color: white; padding: 0.5em; display: inline-block;'>cl-who</span> – <span>1.64 µs</span></div><div style='margin-top: 1em;'><span style='width: 479.61166px; background-color: orange; color: white; padding: 0.5em; display: inline-block;'>spinneret</span> – <span>4.94 µs</span></div><div style='margin-top: 1em;'><span style='width: 505.82526px; background-color: cyan; color: white; padding: 0.5em; display: inline-block;'>cl-mustache</span> – <span>5.21 µs</span></div>
That is because cl-mustache’s compile-template
function does not do the real
compilation.
It only parses the template and returns a lambda which iterates and calls generic functions in runtime during rendering step.
To conclude, use cl-mustache
if you really want to limit the amount of
logic on the frontend.
If you have some other template engines in mind, please, leave comments and I’ll make a review.