Introduction
Every developer has been there: your application works, but it's slow. Users complain, pages take seconds to load, and database queries feel like they're running in slow motion. Performance optimization is often treated as a mystical art, but in reality, it's a systematic process of finding bottlenecks and applying targeted fixes. The key is asking the right questions—and that's where prompts come in.
In this guide, I'll share 10 battle-tested prompts I use daily to identify performance issues in codebases, from profiling to memory leaks to database queries. Each prompt is paired with a real-world example, so you can copy-paste them into your workflow immediately. No fluff, just practical prompts that have saved me hours of debugging.
1. "Profile the slowest function in this module"
When to use: You suspect a specific function is slow but don't know where to start.
Example:
# Your code
def process_orders(orders):
for order in orders:
# ... complex logic
Prompt:
"Given this code, profile the
process_ordersfunction using cProfile and identify the top 3 time-consuming calls. Return the profile output and suggest optimizations."
Why it works: Profiling is the first step in optimization. This prompt forces the AI to run a profiler (or simulate one) and focus on the actual hotspots, not guesses.
2. "Explain why this query is slow and rewrite it"
When to use: A database query takes >200ms.
Example:
SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE signup_date > '2025-01-01');
Prompt:
"Analyze this SQL query for performance issues. Use EXPLAIN ANALYZE output to identify missing indexes or inefficient joins. Suggest a rewritten version with proper indexing strategy."
Why it works: Many slow queries are caused by missing indexes or subquery inefficiencies. The prompt forces the AI to simulate an EXPLAIN ANALYZE and propose concrete fixes.
3. "Find memory leaks in this async code"
When to use: Your application's memory usage grows over time.
Example:
import asyncio
async def handle_connection():
# ... some async handler
await asyncio.sleep(0.1)
Prompt:
"Review this async Python code for potential memory leaks. Check for unclosed event loops, circular references, or growing lists in closures. Use objgraph or tracemalloc patterns to identify the leak source."
Why it works: Async code is prone to subtle leaks. The prompt directs the AI to check common patterns like unclosed connections or accumulating coroutines.
4. "Optimize this React render loop"
When to use: A React component re-renders too often.
Example:
function UserList({ users }) {
return users.map(user => <UserCard key={user.id} user={user} />);
}
Prompt:
"Analyze this React component for unnecessary re-renders. Use React DevTools profiler to identify why
UserCardre-renders whenusersarray reference changes. Suggest memoization withReact.memoanduseMemo."
Why it works: React re-rendering is a common performance pitfall. The prompt asks for specific profiler output and concrete solutions.
5. "Benchmark two algorithm implementations"
When to use: Choosing between two approaches for the same task.
Example:
# Approach A: list comprehension
squares = [x**2 for x in range(1000)]
# Approach B: map with lambda
squares = list(map(lambda x: x**2, range(1000)))
Prompt:
"Benchmark these two approaches using timeit with 10,000 iterations. Return the mean execution time, standard deviation, and explain which is faster and why."
Why it works: Intuition often fails. This prompt forces a data-driven comparison instead of guessing.
6. "Identify N+1 queries in this ORM code"
When to use: You suspect your ORM is generating too many queries.
Example (Django):
books = Book.objects.all()
for book in books:
print(book.author.name)
Prompt:
"Analyze this Django ORM code for N+1 query patterns. Use Django Debug Toolbar output to show the number of queries executed. Suggest
select_relatedorprefetch_relatedoptimizations."
Why it works: N+1 is one of the most common performance killers in web applications. The prompt asks for both diagnosis and a fix.
7. "Optimize this Docker image size"
When to use: Your Docker image is >1GB.
Example:
FROM python:3.12
COPY . /app
RUN pip install -r requirements.txt
Prompt:
"Analyze this Dockerfile for size optimization opportunities. Suggest multi-stage builds, layer caching, and Alpine image alternatives. Show the before/after image sizes using
docker history."
Why it works: Large images slow down deployments. The prompt forces a systematic review of layers and dependencies.
8. "Find redundant API calls in this frontend code"
When to use: Your frontend makes too many network requests.
Example:
async function loadData() {
const user = await fetch('/api/user');
const orders = await fetch('/api/orders');
const products = await fetch('/api/products');
}
Prompt:
"Review this frontend code for redundant or sequential API calls. Suggest batching endpoints, caching responses with SWR/React Query, or parallelizing independent requests. Show a rewritten version with performance improvements."
Why it works: Sequential requests are a common bottleneck. The prompt asks for both detection and a fix.
9. "Profile this Node.js event loop"
When to use: Your Node.js app has high latency.
Example:
app.get('/heavy', (req, res) => {
// CPU-intensive task
let sum = 0;
for (let i = 0; i < 1e8; i++) sum += i;
res.send({ sum });
});
Prompt:
"Analyze this Node.js endpoint for event loop blocking. Use
clinic doctoror0xflamegraph to identify CPU hotspots. Suggest moving the heavy computation to a worker thread or splitting the task into chunks."
Why it works: Node.js is single-threaded; blocking the event loop kills performance. The prompt asks for specific profiling tools and solutions.
10. "Generate a performance optimization checklist"
When to use: You want a systematic review of your entire application.
Prompt:
"Create a performance optimization checklist for a modern web application. Include categories: frontend (bundle size, lazy loading), backend (caching, query optimization), database (indexes, connection pooling), infrastructure (CDN, compression). For each item, provide a tool or metric to measure it."
Why it works: This prompt turns optimization into a repeatable process. Use the checklist during code reviews or before deployments.
Practical Workflow: Combining Prompts
In real projects, I combine prompts. For example:
- Prompt 1 profiles the slow function.
- Prompt 5 benchmarks the fix.
- Prompt 10 ensures nothing else is missed.
This turns AI into a performance coach that guides you through the entire optimization cycle.
Conclusion
Performance optimization doesn't have to be guesswork. These 10 prompts give you a structured way to identify bottlenecks, test solutions, and verify improvements. The key is to always start with profiling—never optimize without data. Use these prompts in your daily workflow, and you'll transform from a developer who "hopes the code is fast" to one who knows exactly why it's fast.
Remember: the best optimization is the one you measure. Save these prompts, run them against your codebase, and watch your performance metrics improve.
Comments