Performance comparison of Phoenix templating engines?

Hello from a new user here. After working with numerous frameworks and templating engines, I learnt that choosing the templating engine can make a sizeable difference in the long run. I’ve grown to like haml and slim for the productivity boost they give over e. g. native Rails template (erb) but at least the “original” haml implementation came with performance penalty. While negligible in some projects, it became a bottleneck in large project with highly complex templates. This needed to be addressed and was eventually alleviated by switching to hamlit (and partially rewriting the templates). Now - how does that look in Phoenix world? Do we have some reliable templating engines benchmarks available?

I do not think that there are benchmarks, but in Phoenix world most of them should matter only in compile time and it should be almost no-problem in runtime as all of them are compiled to IO vectors during compilation and then these are just plain function calls.

2 Likes

Are there even different templating engines available? I thought EEx is the only one right now (Phoenix has an extended version of it that works with IO lists I believe).

I know of a HAML clone (structured HTML templating language) and also I’ve heard about a mustache clone, though haven’t used that, nor do I know how the package is actually named or if it got released on hex.

I know of a handful of packages for various templating languages, but afaik most don’t have big adoption or great/full feature support.

It’s hard to go back after using haml and slim for some time. Yes, there are implementations for Elixir and Phoenix. For the first more serious project in Phoenix I currently chose phoenix-slme. Seems to work so far but I would prefer to know if this doesn’t come with some unpleasant performance surprises later on. Been there, seen that. Luckily in Rails world slim is on a par with the default erb and hamlit (a faster reimplementation of haml) is also more than good enough. It was the original haml that bit me once.

Right, I understand that these are compiled. The thing is that different templating languages are going to be compiled differently. That’s where the differences might kick-in.

There should be almost no difference. The only difference I can see is if you would use the insecure template engine that does no escaping. That could give slight performance boost. Otherwise there should be almost no difference, because, as I said the templates are compiled to the IO vectors, so theoretically EEx file:

<div class="foo"><%= variable %></div>

And HAML file:

.foo= variable

Should compile to the same list:

["<div class=\"foo\">", variable, "</div>"]

It is important to know that templates in Phoenix do not produce binary (Elixir string) but iodata/0 which is defined as iolist() | binary() where iolist() :: maybe_improper_list(byte() | binary() | iolist(), binary() | []). Improper list is a list that has non-list element as tail, so for example ["foo"] is proper list (tail is equal to []) and ["foo" | 10] is improper list, as tail is equal to 10.

2 Likes

@hauleth - tnx