API / Apache Velocity Interview questions
How can you optimize Velocity template rendering performance?
A few practical levers make the most difference in real applications.
- Enable resource caching — set
file.resource.loader.cacheto true in production so parsed templates aren't re-read from disk on every render. - Initialize the engine once — construct and
init()a singleVelocityEngineat startup rather than per request; initialization loads and parses macro libraries. - Reuse a StringBuilder/Writer efficiently — avoid unnecessary intermediate String conversions between merges.
- Keep contexts small — only put the data a template actually needs; building large object graphs it never reads wastes cycles.
- Favor Velocimacros over deeply nested #parse chains — each #parse call re-resolves a resource lookup, while a cached macro is already in memory.
- Avoid expensive method calls inside #foreach — compute a value once with #set before the loop rather than recalculating it every iteration.
Most performance problems in practice trace back to caching being disabled or heavy computation accidentally happening inside the template rather than in Java.
More Related questions...