0
0
Expressframework~8 mins

PUT and PATCH route handling in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: PUT and PATCH route handling
MEDIUM IMPACT
This affects server response time and client perceived latency when updating resources via HTTP routes.
Updating a resource partially versus fully replacing it
Express
app.patch('/user/:id', (req, res) => {
  // expects partial user data
  const partialUpdate = req.body;
  // update only provided fields
  db.updateUserFields(req.params.id, partialUpdate);
  res.sendStatus(200);
});
PATCH sends only changed fields, reducing payload size and server processing time, improving response speed.
📈 Performance Gainreduces network payload size by up to 80% for small updates; faster server processing
Updating a resource partially versus fully replacing it
Express
app.put('/user/:id', (req, res) => {
  // expects full user object
  const updatedUser = req.body;
  // replace entire user in DB
  db.updateUser(req.params.id, updatedUser);
  res.sendStatus(200);
});
PUT expects full resource replacement, sending large payloads even for small changes, causing unnecessary processing and network load.
📉 Performance Costblocks response for full data processing; larger payloads increase network latency
Performance Comparison
PatternPayload SizeServer ProcessingNetwork LatencyVerdict
PUT full updateLarge (full resource)High (process entire object)High (larger data sent)[X] Bad
PATCH partial updateSmall (only changes)Low (process few fields)Low (smaller data sent)[OK] Good
Rendering Pipeline
When a PUT or PATCH request is made, the server processes the payload, updates the database, and sends a response. The client then updates the UI based on the response. Smaller payloads and faster processing reduce the time before the UI updates, improving interaction responsiveness.
Network Transfer
Server Processing
Client Rendering
⚠️ BottleneckServer Processing time increases with larger payloads and full resource replacements.
Core Web Vital Affected
INP
This affects server response time and client perceived latency when updating resources via HTTP routes.
Optimization Tips
1Use PATCH for partial updates to minimize data sent and processed.
2Avoid using PUT for small changes to reduce network and server load.
3Monitor request payload size and response time in DevTools Network panel.
Performance Quiz - 3 Questions
Test your performance knowledge
Which HTTP method is better for sending only changed data to update a resource?
AGET
BPATCH
CPUT
DDELETE
DevTools: Network
How to check: Open DevTools, go to Network tab, perform the update request, and inspect the request payload size and response time.
What to look for: Look for smaller request payloads and faster response times indicating efficient PATCH usage.