Performance optimization with GitHub Copilot
1. Performance optimization with GitHub Copilot
Welcome to another video! Your code is now tested and secure. But there's one more dimension that can bring an application down, and it's the one that's easy to miss: performance.2. The silent slowdown
Performance issues are sneaky. They don't show up in unit tests and they don't cause immediate crashes. They just make your application feel broken: a page that takes too long to load, a search that spins forever. And the difficult part? Slow code often looks correct. It just doesn't scale.3. A bottleneck in the wild
Here's a classic example. This function finds common elements between two lists by comparing every item in list1 against every item in list2. For small inputs, it works perfectly. But scale it up to a million items each and you're looking at a trillion comparisons. That's an O of n-squared algorithm — the kind that sails through code review, runs fine in development, and brings down production on the first busy day.4. Asking Copilot to optimize
Let's fix it. Select the function, open the Copilot chat, and ask: "Optimize this function for performance." Copilot identifies the bottleneck, explains why nested loops are expensive here, and rewrites the function using a more efficient approach. Unlike a linter, it doesn't just flag the problem, it hands you the solution.5. Before and after
Here's the result. The original compares every pair — O of n-squared. Copilot rewrites it with a set intersection — one line, O of n. For a million items, that's the difference between a fraction of a second and several minutes. And the intent of the code is now clearer too — set intersection is exactly what the function was always trying to do.6. Tell Copilot what to optimize for
That nested loop is just one example. Copilot can spot a much wider range of patterns, and the key is telling it which dimension matters most. Optimizing for speed? It'll suggest faster algorithms, caching, or batching. For memory? It'll flag resource leaks, suggest generators, or replace in-memory loads with streaming. And if both matter, just say so, Copilot can review across all of these at once.7. Performance prompts that work
Just like with security, the real skill is knowing which prompt to reach for. For a specific function, select it and ask Copilot to optimize for performance, or narrow it down to a specific memory requirement. For a broader sweep, use @workspace to scan your codebase for expensive patterns across all your files.8. Tailoring optimization prompts
Here's that specificity in action. Notice how a targeted prompt produces a much sharper response than a vague "optimize this." When Copilot knows exactly what you're trying to improve, it skips the generic suggestions and goes straight to what matters.9. Catching resource leaks
Here's another example of Copilot in action, catching resource leaks. Ask it to review your code for resource management and it'll suggest context managers, flag unreleased connections, and apply cleanup patterns consistently.10. Bake performance into every suggestion
And just like with security, the most powerful approach is prevention. Add performance standards to your copilot-instructions.md — rules like "prefer set lookups over nested loops," "use context managers for all resources," or "avoid loading entire datasets into memory." Copilot applies these on every suggestion. You're no longer just fixing bottlenecks after the fact — you're preventing them from being written.11. Let's practice!
Your code is now tested, secure, and fast. Now it's your turn to put these performance skills into practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.