0
0
Expressframework~8 mins

Finding and querying documents in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Finding and querying documents
MEDIUM IMPACT
This affects server response time and how quickly the client receives data after a query.
Querying documents from a database in an Express app
Express
app.get('/users', async (req, res) => {
  const users = await User.find({ active: true }).limit(50);
  res.json(users);
});
Filters data and limits results to reduce server load and response size.
📈 Performance GainFaster query execution and smaller payload; improves LCP.
Querying documents from a database in an Express app
Express
app.get('/users', async (req, res) => {
  const users = await User.find();
  res.json(users);
});
Fetching all documents without filters can return large data sets, increasing response time and payload size.
📉 Performance CostBlocks response until all documents load; large payload increases network time.
Performance Comparison
PatternServer ProcessingNetwork PayloadResponse TimeVerdict
Query all documents without filtersHigh (full collection scan)Large (all data sent)Slow (blocks response)[X] Bad
Query with filters and limitLow (uses indexes)Small (limited data)Fast (quick response)[OK] Good
Rendering Pipeline
Express queries run on the server before sending data to the browser. Slow queries delay the server response, which delays the browser's rendering start.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing (database query execution)
Core Web Vital Affected
LCP
This affects server response time and how quickly the client receives data after a query.
Optimization Tips
1Always filter queries to return only needed documents.
2Use database indexes to speed up queries.
3Limit the number of documents returned to reduce payload size.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a common performance issue when querying documents without filters in Express?
AReturning too many documents increases response time and payload size.
BUsing filters slows down the query.
CLimiting results causes server errors.
DQueries without filters always use indexes.
DevTools: Network
How to check: Open DevTools, go to Network tab, filter by XHR/fetch, and inspect the API request timing and payload size.
What to look for: Look for long waiting times or large response sizes indicating slow or heavy queries.