Thymeleaf performance

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymeleaf performance

Christian Bjørnbak
Measuring Thymeleaf's performance with https://github.com/mbosecke/template-benchmark shows that Thymeleaf is much slower that the competition.

The test results shown on github.com is based on Thymeleaf 2.1.4.

I have forked the code and upgraded to Thymeleaf 3.0.0-RELEASE.

Now I can measure 5x more operations/second but still Thymeleaf only performs a fraction of the competitors.

Is the test broken or do Thymeleaf perform this bad?

The fastest frameworks Pebble and Rocker the precompiles their templates into Java code is that a possibility for Thymeleaf or do you have any other optimizations in the plans?
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf performance

danielfernandez
Administrator
Yes, that benchmark has some fundamental flaws. I have in my TO-DO submitting a PR or at least some comments at some point in the future. Not that it has been specifically prepared against Thymeleaf or anything -- it's simply that the author of the benchmark (who is also the author of the Pebble template engine) took a couple of wrong performance-wise decisions regarding Thymeleaf. And of course the lack of a 3.0 version of the benchmark avoids the use of features such as the new SpringEL compiler in Spring-enabled apps or the TEXT template mode if we wanted to really compare in fairness with the others (which are text --not markup-- template processors).

Also, in some aspects the Thymeleaf template simply does much more than some of the others (e.g. conditional expressions, use of a powerful expression language like OGNL, template-level comments or comparing with a Mustache template which has almost all its logic in Java classes...).

Besides that, the benchmarked template is specifically restricted to not do things that in the real world it should do, that are easy/transparent in Thymeleaf, and that with some of the other engines would be complex (or unavailable depending on the case): transparent URL rewriting, escaping, etc.

This said, Thymeleaf IS slower than the other template engines being tested in at least a good percent of scenarios. But the main reason for this is the different nature of the technology: Thymeleaf is not a mere text processor that quickly scans through a stream of text looking for some specific directives, applies them, writes to output, and goes on scanning (that's how JSP, FreeMarker, Velocity, etc. work). Instead, Thymeleaf parses markup and *understands* it, which allows it to use markup itself as the container of its logic and apply concepts such as natural templates if the developer wants/needs. Also, it offers an extremely powerful, fully-featured expression language (OGNL or SpringEL depending on the setup), a large amount of in-template tools, integrated i18n and URL rewriting, advanced layout capabilities (such as fragment expressions), specific Spring Framework integrations (IMHO the best ones out there), decoupled templates, and even capabilities like engine throttling which allow the technology to be integrated seamlessly into server-side reactive data flows.

So basically, Thymeleaf works at a different abstraction level than most other template engines, something that (I hope, though this is a matter of personal taste) shows in the elegance of its code and the completeness of its feature set. IMHO therefore making a performance-only comparison with other template engines might be a bit unfair, a little bit --just a little bit, admittedly-- like comparing the performance of JPA against JDBC raw usage.

Also, think that what we are comparing here is the performance of the view layer only. A layer that in most applications will mean an almost-negligible part of the overall application execution time.

And last, of course I understand there might be an amount of applications in which the performance of the view layer will be so important that the advantages of using Thymeleaf will completely dilute. Or even cases in which the advantages of Thymeleaf will simply not apply or be required at all. In such cases, fortunately, there a bunch of other options, some of them, not joking, really great :-)

Regards,
Daniel.


Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf performance

Christian Bjørnbak
Hi Daniel,

I understand you're saying that because Thymeleaf is much more powerful we can expect less performance.

But in these comparisons the difference is very significant. Even after upgrading to Thymeleaf 3 I still see a factor 6-8 difference compared to Pebble and Rocker and a factor 3 difference to FreeMarker and Velocity.

I see you thing you believe Thymeleaf are more powerful than FreeMarker and Velocity but to me all three are parsing the templates runtime while Pebble and Rocker precompiles the template into Java code.

I would expect similar performance of Thymeleaf, FreeMarker and Velocity.

Can't something similar to Pebble and Rocker's precompilation be done with Thymeleaf?

I'm asking because we are trying to get out response time below Google Pagespeed's magic line of 200 ms. We have optimizing our code, db queries etc and have stranded where our JSP view layer consumes most of the response time.

I'm looking for a replacement and loves that the natural templating but can't risk increasing the response times..
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf performance

danielfernandez
Administrator
> I understand you're saying that because Thymeleaf is much more powerful we can expect less performance.

No, not at all. Of course not. I'm saying Thymeleaf is a different nature of a template engine, by architecture and construction, to the others that are being compared in that benchmark. And that the benchmark is real-world incomplete and therefore a bit unfair.

> Can't something similar to Pebble and Rocker's precompilation be done with Thymeleaf?

No it cannot.

> I'm asking because we are trying to get out response time below Google Pagespeed's magic line of 200 ms. We have optimizing our code, db queries etc and have stranded where our JSP view layer consumes most of the response time.

In your case, a JSP layer that consumes most of 200ms response times in production servers definitely looks like an extremely complex and heavy template set to me -- or maybe its an issue with slow JSP tag libs, not JSP itself?

Anyway, if you have such view-layer-specific performance restrictions, and given the apparent complexity of your view layer, I would definitely not recommend you using Thymeleaf.

Regards,
Daniel.