5 min read

Lets compare Spring boot template engines

September 05, 2020

Spring Boot supports FreeMarker, Groovy, Mustache and Thymeleaf. But how do they compare with each other? Let’s find out.

For this comparison, I have created four spring boot projects and added them to a parent project. This way, I can easily control dependencies for all four sub-projects. Also, I can build all of them at once. You can find the GitHub project at the end of this post.

Build Size

Size matters when it comes to application deployment. Smaller builds are more comfortable to transfer between the CI platform to production servers. Let’s compare the built jar file size. To do this, I ran the maven clean package on the parent project. Here are the results.

Startup time of Spring boot with different template engines

As you can see, Mustache is the clear winner having about 16MB jar followed by Thymeleaf. FreeMarker is not that behind Thymeleaf while Groovy is far behind by having 21.4 MB size.

Let’s test the startup time of each application. This test is one of the critical aspects of a microservice. But the application startup time may be influenced by many factors. So I had to run this test many times to remove any bias with the machine I was testing.

Application Boot-up time

Startup time of Spring boot with different template engines

This graph shows us the Startup time of each variation of our spring boot application. Even though the gap between different template engines is only about 1 second, The real-world difference would be far higher.

Mustache wins this round by never leaving the 6-second mark. The worst performer is Groovy template engine. I think this has something to do with the JAR size. This engine has higher startup time out of all four. FreeMarker and Thymeleaf are going neck to neck. But FreeMarker slightly has the upper hand in this area.

Performance benchmarks

Now Let’s compare the performance of the engines itself. We are going to render the following sample in each template engine.

Sample content to be generated by Engines

For the benchmark test, The template caching mechanism is disabled for all four templateEngines.

Here are the Apache BenchMark results. I made this graph on a logarithmic scale because of how worse the Groovy results were.

Apache Benchmark results for Template Engines

Speaking of the other three engines, All of them performed similarly. FreeMarker completed 1000 requests in 135.002 milliseconds while Mustache took 134.002 milliseconds. Thymeleaf was slightly off with 154.998 milliseconds run time.

So far, Mustache and FreeMarker are doing very well. Thymeleaf is not far behind. Groovy is way out of its league.

Let’s look at some other aspects of these template engines.

Extensibility

In terms of customization, Thymeleaf is the best of all four. By using templateEngine.addDialect() developers can supply their dialects on top of existing dialects.

There aren’t many options available for FreeMarker and Groovy. Mustache has some extensibility at the expense of complicated workarounds.

Documentation

Groovy and Mustache have the most inadequate documentation. Thymeleaf has excellent popularity and great documentation. There are various tutorials available for thymeleaf on the internet. Documentation is available for FreeMarker. But they are not that healthy.

Learning curve

FreeMarker takes the first place for this category. The syntax of FreeMarker is clean and readable. Anyone who understands HTML can understand FreeMarker templates. Second place goes to thymeleaf due to use of natural language and rich support for syntax highlighting in various IDEs.

Mustache is much similar to FreeMarker. But it uses symbols like # ^ / instead of natural words like if-else.

Unlike the other three, Groovy uses different notation altogether to define the template. This notation makes it hard for someone to learn this template. For example, Here is a snippet that generates Grand total of the order in groovy template.

    div(class: "flex flex-row-reverse p-5") {
        h2(class: "font-medium bg-gray-200 p-2 rounded") {
            yield "Grand Total: "
            span(class: "text-green-600", "$orderEntry.payment.amount")
        }
    }

Same can be as simple as the following in FreeMarker

    <div class="flex flex-row-reverse p-5">
        <h2 class="font-medium  bg-gray-200 p-2 rounded">
            Grand Total: <span class="text-green-600">${orderEntry.payment.amount}</span>
        </h2>
    </div>

IDE support

IDE support for template files

Thymeleaf and FreeMarker has substantial support in the world of IDEs. Mustache gets its IDE support through plugins with no variable support. Groovy templates only have Groovy base language support. It also doesn’t have any context variable highlighting.

Usability

Mustache templates will not evaluate simple inline evaluations. For instance, if you need to add two numbers and print the result within the template result, you can’t. At least not without some complicated workarounds. For this reason, I mark Mustache as the least usable template engine.

Groovy templates seem a little off from the others since the beginning in every aspect. Both from performance and learning standpoint, Groovy is out of this topic. Also it doesn’t add any new value compared to FreeMarker and Thymeleaf.

FreeMarker has all the essential features to generate an HTML for most of the cases. And it is easier to learn. So it is not a bad idea if you want to use it. But, Thymeleaf is the way to go if you want to add custom functionality to your templates.

Any thoughts?

Raja Anbazhagan

About the author

Raja is a Software Engineer with over 7 years of experience in working with Enterprise Java applications. Lately, He is focused in cloud-based Java applications and serverless technologies. He spends his spare time in stackoverflow.

Browse Categories