API / Apache FreeMarker Interview questions
How can you optimize FreeMarker template performance?
Most performance problems come from re-doing work FreeMarker is already designed to cache, or from expensive calls repeated inside loops. Practical steps include:
- Reuse one Configuration for the whole application instead of creating a new one per request, since it owns the parsed-template cache.
- Let template caching work - avoid disabling it, and set a sensible
templateUpdateDelayMillisecondsinstead of checking the source on every single request. - Hoist expensive lookups out of loops. If a value inside a
<#list>is computed by a costly bean method, compute it once in Java and pass it in, rather than calling it once per iteration from the template. - Choose the right TemplateLoader.
ClassTemplateLoaderavoids per-request filesystem stats; aMultiTemplateLoaderwith many entries adds lookup overhead for every miss. - Reduce output size with appropriate whitespace-stripping settings so less text is generated and written per request.
In practice, the biggest wins usually come from cache configuration and avoiding repeated expensive Java calls inside loops, not from micro-optimizing the FTL syntax itself.
More Related questions...