Performance: EJS template setup
This affects the server-side rendering speed and initial page load time by how templates are compiled and served.
const cachedData = heavyComputation(); app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.render('index', { data: cachedData }); });
app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.render('index', { data: heavyComputation() }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Render with heavy computation on each request | N/A (server-side) | N/A | Delays initial paint | [X] Bad |
| Render with precomputed data and cached templates | N/A (server-side) | N/A | Fast initial paint | [OK] Good |
app.set('view engine', 'ejs') do in an Express app?app.setindex.ejs with Express?res.render with the template name without extension.app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('greeting', { name: 'Sam' });
});greeting.ejs file contains:<h1>Hello, <%= name %>!</h1>
<%= name %> outputs the value of 'name' safely, so it becomes 'Sam'.const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('home.ejs');
});users.ejs?const users = ['Anna', 'Bob', 'Cara'];
app.get('/users', (req, res) => {
// What goes here?
});// What goes here?.Pass the test and this pattern moves unknown