Performance: Passing data to templates
This affects the server response time and the browser's initial rendering speed by controlling how much data is sent and how templates are processed.
app.get('/profile', (req, res) => { const user = getUserFromDb(); res.render('profile', { user }); });
app.get('/profile', (req, res) => { const user = getUserFromDb(); res.render('profile', { user: user, allUsers: getAllUsersFromDb() }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing all data to template | High (large DOM from extra data) | Multiple reflows due to large content | High paint cost | [X] Bad |
| Passing minimal required data | Low (smaller DOM) | Single reflow | Low paint cost | [OK] Good |
res.render() usageres.render() to render templates and can accept data as the second argument.res.render() -> Option Dres.render() object = By passing an object as the second argument to res.render() [OK]res.render(view, dataObject) to pass data [OK]req instead of res.render()title with value 'Home' to a template named index?res.render() parameters{ title: 'Home' } correctly sends the variable title with value 'Home'.app.get('/', (req, res) => {
res.render('home', { user: 'Alice', age: 30 });
});home template access?res.render() has keys user and age with values 'Alice' and 30.user with 'Alice' and age with 30 -> Option Auser with 'Alice' and age with 30 [OK]app.get('/profile', (req, res) => {
res.render('profile', user: 'Bob');
});res.render() syntaxuser: 'Bob' is passed without braces, causing syntax error.res.render() [OK]dashboard that shows a user's name and a list of their tasks. Which code correctly passes this data to the template?name is a string and tasks is an array, both correctly passed in res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] });.Pass the test and this pattern moves unknown