Performance: PUT and DELETE requests
This affects the network request time and how quickly the UI updates after data changes.
this.http.put('/api/items/' + id, data).subscribe(() => { this.items = this.items.map(item => item.id === id ? {...item, ...data} : item); }); this.http.delete('/api/items/' + id).subscribe(() => { this.items = this.items.filter(item => item.id !== id); });
this.http.put('/api/items/' + id, data).subscribe(() => { this.loadAllItems(); }); this.http.delete('/api/items/' + id).subscribe(() => { this.loadAllItems(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Reload all data after PUT/DELETE | Many DOM updates | Multiple reflows | High paint cost | [X] Bad |
| Update local state after PUT/DELETE | Minimal DOM updates | Single reflow | Low paint cost | [OK] Good |
PUT request in Angular's HttpClient?delete() to send DELETE requests.this.http.delete('api/items/1').subscribe(); correctly sends a DELETE request and subscribes to the response.const updatedItem = { id: 5, name: 'New Name' };
this.http.put('api/items/5', updatedItem).subscribe(response => {
console.log(response);
});response logged is the server's reply, typically the updated item object.this.http.delete('api/items').subscribe(response => {
console.log('Item deleted');
});// Update user profile
this.http.put('api/users/10', updatedUser).subscribe(() => {
// Delete old profile picture
this.http.delete('api/pictures/oldPicId').subscribe(() => {
console.log('Profile updated and old picture deleted');
});
});Pass the test and this pattern moves unknown