Performance: CRUD operations
CRUD operations impact server response time and client rendering speed, affecting how quickly users see updates and interact with data.
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/update', methods=['POST']) def update(): data = request.json['data'] # Update database asynchronously or quickly update_database_async(data) return jsonify({'status': 'success', 'data': data})
from flask import Flask, request, render_template app = Flask(__name__) @app.route('/update', methods=['POST']) def update(): data = request.form['data'] # Update database synchronously update_database(data) # blocking call return render_template('page.html', data=data)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous full page reload after CRUD | High (full DOM reload) | Multiple reflows | High paint cost | [X] Bad |
| Asynchronous JSON response with client-side update | Low (partial DOM update) | Single reflow | Low paint cost | [OK] Good |
/item/5?
from flask import Flask, jsonify
app = Flask(__name__)
items = {1: 'apple', 5: 'banana'}
@app.route('/item/', methods=['DELETE'])
def delete_item(id):
if id in items:
del items[id]
return jsonify({'message': 'Deleted'}), 200
else:
return jsonify({'error': 'Not found'}), 404
@app.route('/item', methods=['POST'])
def create_item():
data = request.json
items.append(data['name'])
return jsonify({'message': 'Item added'}), 201
@app.route('/item/', methods=['PUT'])
def update_item(id):
data = request.json
items[id] = data['name']
return jsonify({'message': 'Updated'}), 200
B) @app.route('/item/', methods=['PUT'])
def update_item(id):
if id not in items:
return jsonify({'error': 'Not found'}), 404
data = request.json
items[id] = data['name']
return jsonify({'message': 'Updated'}), 200
C) @app.route('/item/', methods=['POST'])
def update_item(id):
data = request.json
if id in items:
items[id] = data['name']
return jsonify({'message': 'Updated'}), 200
D) @app.route('/item/', methods=['PUT'])
def update_item(id):
data = request.json
if id in items:
items[id] = data['name']
else:
items[id] = 'default'
return jsonify({'message': 'Updated'}), 200Pass the test and this pattern moves unknown