Performance: Why architectural patterns matter
Architectural patterns affect how quickly the server responds and how efficiently it handles requests, impacting overall page load speed and user experience.
const express = require('express'); const app = express(); // Separate service layer handles DB calls asynchronously async function getData() { return await database.query('SELECT * FROM table'); } app.get('/data', async (req, res) => { const data = await getData(); res.send(data); }); app.listen(3000);
const express = require('express'); const app = express(); app.get('/data', (req, res) => { // All logic and database calls directly inside route handler const data = database.query('SELECT * FROM table'); res.send(data); }); app.listen(3000);
| Pattern | Server Blocking | Event Loop Impact | Response Time | Verdict |
|---|---|---|---|---|
| Monolithic route handlers with sync DB calls | High | Blocks event loop | Slow | [X] Bad |
| Modular async service layers | Low | Non-blocking | Fast | [OK] Good |
app.js imports routes.js
routes.js calls controller functions
controller.js handles logic and calls model.js
model.js accesses database
const express = require('express');
const app = express();
app.get('/user', (req, res) => {
res.send('User page');
});
app.listen(3000);
module.exports = app;Pass the test and this pattern moves unknown