Performance: Flask route as API endpoint
This affects server response time and client perceived load speed when fetching data from the backend.
from flask import Flask, jsonify app = Flask(__name__) @app.route('/data') def data(): return jsonify({'message': 'Hello'})
from flask import Flask app = Flask(__name__) @app.route('/data') def data(): import time time.sleep(2) # Simulate slow processing return {'message': 'Hello'}
| Pattern | Server Processing | Response Delay | Network Payload | Verdict |
|---|---|---|---|---|
| Slow blocking route with delay | High CPU wait | 2 seconds delay | Minimal JSON size | [X] Bad |
| Fast immediate JSON response | Minimal CPU time | Near zero delay | Minimal JSON size | [OK] Good |
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/hello')
def hello():
return jsonify({'message': 'Hello, world!'})from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/data')
def data():
return json({'key': 'value'})Pass the test and this pattern moves unknown