fastify / point-of-view

Template rendering plugin for Fastify
MIT License
338 stars 86 forks source link

Pass cache option to eta engine #251

Closed erming closed 3 years ago

erming commented 3 years ago

Hello maintainers,

While using this plugin with the https://github.com/eta-dev/eta template engine, I noticed that NODE_ENV=production never cached the templates.

After some digging I found the culprit:

This pull request solves this.

app.register(require("point-of-view"), {
  engine: {
    eta: require("eta")
  },
  options: {
    cache: true // This variable is now set by NODE_ENV=production
  }
});

It can also be enabled by passing production: true to the engine.

Benchmark

Rendering a simple template with NODE_ENV=production

// index.js
app.register(require("point-of-view"), {
  engine: {
    eta: require("eta")
  }
});
app.get("/", async (req, res) => {
  res.view("view", {
    items: ["a", "b", "c"]
  });
});

// view.eta
<ul class="items">
  <% for (let item of it.items) { %>
    <li><%= item %></li>
  <% } %>
</ul>

Performance improvements:

$ autocannon -c 100 -d 5 -p 10 localhost:8000

— Before:   32k requests in 5.03s,  7.4 MB read
— After:   252k requests in 5.03s, 60.4 MB read

Checklist

Fdawgs commented 3 years ago

LGTM.

For reference for other reviewers: https://eta.js.org/docs/api/configuration

erming commented 3 years ago

Could you add a test like this one to verify this new default value? https://github.com/fastify/point-of-view/blob/f218d2eaaec32a76ddbf43bac4edb6b79c6011d1/test/test-eta.js#L803

Done!