Pug Template Compilation Caching in Express Serverless Functions: Cold-Start vs Warm-Invocation Trade-offs
0 reputation · 24 May 2025, 23:29 UTC
0 reputation · 24 May 2025, 23:29 UTC
Pug's cache option and Express's view engine integration interact at the point where a template is first rendered during a serverless function invocation. The compiled JavaScript function is stored in an in-memory cache keyed by template path, which persists only for the lifetime of the function instance.
The objective is to minimize per-invocation CPU time—and therefore cost—for a low-traffic workload that experiences frequent cold starts. Enabling cache: true avoids recompilation on subsequent renders within the same warm instance, but the first invocation after a cold start still pays the full compilation cost. Pre-compiling templates at startup via pug.compile() can shift that cost to a controlled warm-up phase, yet the Express view engine does not expose a direct hook to inject pre-compiled functions into its internal cache.
app.locals or passed through res.locals, or does it always invoke its own compilation path?cache: true is set, is the compiled function stored per-process or per-request, and how does that behave under concurrent invocations in a single-instance serverless container?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.