0
0
Expressframework~8 mins

Mocking database calls in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Mocking database calls
MEDIUM IMPACT
This concept affects server response time and user experience by reducing delays caused by real database queries during development and testing.
Testing API endpoints without hitting the real database
Express
const mockUsers = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
app.get('/users', async (req, res) => {
  res.json(mockUsers);
});
Returns data instantly without waiting for database, improving test speed and reliability.
📈 Performance Gainresponse time reduced to <1ms, no DB wait
Testing API endpoints without hitting the real database
Express
app.get('/users', async (req, res) => {
  const users = await db.query('SELECT * FROM users');
  res.json(users);
});
This calls the real database on every request, causing slow responses and possible test flakiness.
📉 Performance Costblocks response for 50-200ms per request depending on DB speed
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Real DB call on each requestN/A (server-side)N/AN/A[X] Bad
Mocked DB call with static dataN/A (server-side)N/AN/A[OK] Good
Rendering Pipeline
Mocking database calls bypasses the backend database query stage, allowing the server to respond faster. This reduces the time before the browser receives data and can render content.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing (database query time)
Core Web Vital Affected
INP
This concept affects server response time and user experience by reducing delays caused by real database queries during development and testing.
Optimization Tips
1Mock database calls to avoid slow real queries during development and testing.
2Use static or in-memory data to speed up API responses.
3Faster API responses improve user interaction responsiveness (INP).
Performance Quiz - 3 Questions
Test your performance knowledge
Why does mocking database calls improve API response times during development?
ABecause it compresses the network data
BBecause it reduces the size of the database
CBecause it avoids waiting for slow database queries
DBecause it caches the browser rendering
DevTools: Network
How to check: Open DevTools, go to Network tab, make the API request, and check the response time.
What to look for: Look for faster response times (<1ms) when mocking versus slower times (50ms+) with real DB calls.