Performance: JWT token creation
MEDIUM IMPACT
This affects server response time and initial page load speed when tokens are generated and sent to clients.
import jwt from 'jsonwebtoken'; app.post('/login', async (req, res) => { const user = await getUserFromDbAsync(req.body.username); jwt.sign({ id: user.id }, 'secretkey', { expiresIn: '1h' }, (err, token) => { if (err) return res.status(500).send('Error creating token'); res.send({ token }); }); });
const jwt = require('jsonwebtoken'); app.post('/login', (req, res) => { const user = getUserFromDb(req.body.username); const token = jwt.sign({ id: user.id }, 'secretkey', { expiresIn: '1h' }); res.send({ token }); });
| Pattern | Server Blocking | Response Delay | Network Impact | Verdict |
|---|---|---|---|---|
| Synchronous JWT creation | Blocks event loop | Adds 50-100ms delay | Minimal (small token size) | [X] Bad |
| Asynchronous JWT creation | Non-blocking | Minimal delay | Minimal (small token size) | [OK] Good |