Performance: User registration flow
This affects the server response time and user interaction speed during registration, impacting how quickly the page loads and responds.
Jump into concepts and practice - no test required
app.post('/register', async (req, res) => { const user = req.body; // Asynchronous password hashing const hashedPassword = await bcrypt.hash(user.password, 10); // Asynchronous database write await db.saveUser({ ...user, password: hashedPassword }); res.send('User registered'); });
app.post('/register', (req, res) => { const user = req.body; // Synchronous password hashing (blocking) const hashedPassword = bcrypt.hashSync(user.password, 10); // Synchronous database write db.saveUser({ ...user, password: hashedPassword }); res.send('User registered'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking operations | Minimal | 0 | Low | [X] Bad |
| Asynchronous non-blocking operations | Minimal | 0 | Low | [OK] Good |
app.post('/register', ...) in an Express user registration flow?app.post is used to handle POST requests, which usually send data to the server.express.json() middleware to parse JSON request bodies.app.use() to apply globally or on specific routes.app.post('/register', (req, res) => {
const { username } = req.body;
if (users.includes(username)) {
res.status(400).send('User exists');
} else {
users.push(username);
res.status(201).send('User created');
}
});username is in users array and sends status 400 with 'User exists' if true.app.post('/register', (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
res.status(400).send('Missing fields');
}
users.push({ username, password });
res.status(201).send('User registered');
});return, the code continues and tries to push user and send another response, causing an error.app.post('/register', (req, res) => {
const { username, password } = req.body;
// Your code here
});users.includes(username). If true, it responds with status 400 'User exists' and returns to prevent duplicates.password.length < 8. If true, responds with 400 'Password too short' and returns.users.push({ username, password }) and sends 201 'User registered'.