0
0
Expressframework~8 mins

Updating documents in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Updating documents
MEDIUM IMPACT
This affects server response time and client perceived latency when updating data that triggers UI changes.
Updating a document in a database and sending the updated data to the client
Express
app.put('/item/:id', async (req, res) => {
  const id = req.params.id;
  const update = req.body;
  const result = await db.collection('items').findOneAndUpdate(
    {_id: new ObjectId(id)},
    {$set: update},
    {returnDocument: 'after'}
  );
  res.send(result.value);
});
Single atomic database call reduces latency and server load.
📈 Performance GainSaves 50-100ms per request by avoiding redundant DB fetch
Updating a document in a database and sending the updated data to the client
Express
app.put('/item/:id', async (req, res) => {
  const id = req.params.id;
  const update = req.body;
  const doc = await db.collection('items').findOne({_id: new ObjectId(id)});
  Object.assign(doc, update);
  await db.collection('items').updateOne({_id: new ObjectId(id)}, {$set: doc});
  res.send(doc);
});
Fetching the document first then updating causes two database operations and delays response.
📉 Performance CostBlocks response for extra 50-100ms due to double DB calls
Performance Comparison
PatternDB CallsServer DelayNetwork PayloadVerdict
Fetch then update2 calls (findOne + updateOne)High (extra 50-100ms)Larger (full doc sent back)[X] Bad
Atomic findOneAndUpdate1 callLow (minimal delay)Smaller (only updated doc)[OK] Good
Rendering Pipeline
Updating documents on the server affects the time until the client receives updated data and can re-render UI. The server processing delays INP (input responsiveness) as the client waits for confirmation.
Server Processing
Network Transfer
Client Rendering
⚠️ BottleneckServer Processing due to multiple DB calls or inefficient updates
Core Web Vital Affected
INP
This affects server response time and client perceived latency when updating data that triggers UI changes.
Optimization Tips
1Use atomic update operations to reduce database calls.
2Send minimal updated data to the client to reduce network payload.
3Batch updates when possible to avoid multiple server round-trips.
Performance Quiz - 3 Questions
Test your performance knowledge
Which update pattern reduces server processing time when updating a document?
ASending the entire document back to the client after update
BUsing a single atomic update operation like findOneAndUpdate
CFetching the document first, then updating it separately
DUpdating the document multiple times in a loop
DevTools: Network
How to check: Open DevTools Network tab, perform the update request, and observe the request duration and response size.
What to look for: Look for longer request times and larger response payloads indicating inefficient updates.