Performance: Manual validation patterns
Manual validation patterns affect server response time and user experience by adding synchronous or asynchronous checks before processing requests.
Jump into concepts and practice - no test required
function validateInput(data) {
if (!data.email || !data.password) return 'Missing fields';
if (data.password.length < 8) return 'Password too short';
return null;
}
app.post('/submit', (req, res) => {
const error = validateInput(req.body);
if (error) {
res.status(400).send(error);
return;
}
res.send('Success');
});app.post('/submit', (req, res) => { if (!req.body.email || !req.body.password) { res.status(400).send('Missing fields'); return; } if (req.body.password.length < 8) { res.status(400).send('Password too short'); return; } // further processing res.send('Success'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual inline validation with multiple response calls | 0 (server-side) | 0 | 0 | [X] Bad |
| Centralized synchronous validation function | 0 (server-side) | 0 | 0 | [OK] Good |
| Inline async validation with multiple awaits | 0 (server-side) | 0 | 0 | [X] Bad |
| Encapsulated async validation function | 0 (server-side) | 0 | 0 | [OK] Good |
!req.body.username checks if username is missing or empty string.app.post('/login', (req, res) => {
if (req.body.password.length < 8) {
res.status(400).send('Password too short');
}
res.send('Login successful');
});return, the code continues and sends 'Login successful' response, causing error.if (!req.body.email || typeof req.body.email !== 'string') checks for missing, empty, or non-string email and returns 400 error if invalid.if (!req.body.password || req.body.password.length < 8) checks for missing or short password (<8 chars) and returns 400 error.if (req.body.age !== undefined && (typeof req.body.age !== 'number' || req.body.age < 13)) checks if age provided, then ensures it's a number >=13, returns 400 if invalid.