Cursor often gives its first solution attempt without considering performance. You can request optimized alternatives within the same Chat session by asking Cursor to analyze the time/space complexity of its initial suggestion and then generate a faster version. This tutorial shows prompt techniques for iterative optimization without starting over or losing context.
Getting better optimizations from Cursor
Cursor's first response optimizes for correctness and readability, not performance. For critical code paths, you need to explicitly request performance analysis and alternatives. This tutorial teaches a structured workflow for iterating toward optimal solutions within a single Cursor session.
Prerequisites
- Cursor installed with a project open
- Code that needs performance optimization
- Basic understanding of Big-O notation
- Familiarity with Cursor Chat (Cmd+L)
Step-by-step guide
Get the initial solution from Cursor
Get the initial solution from Cursor
Start by getting a working solution. Do not mention optimization in the first prompt. This gives you a correct baseline to improve from.
1// Cursor Chat prompt (Cmd+L):2// Write a function that finds all pairs of numbers in an3// array that sum to a target value. Return an array of4// [index1, index2] pairs.56// Cursor's first attempt (typically O(n^2)):7function findPairs(nums: number[], target: number): number[][] {8 const pairs: number[][] = [];9 for (let i = 0; i < nums.length; i++) {10 for (let j = i + 1; j < nums.length; j++) {11 if (nums[i] + nums[j] === target) {12 pairs.push([i, j]);13 }14 }15 }16 return pairs;17}Expected result: A correct but potentially unoptimized solution.
Ask for complexity analysis
Ask for complexity analysis
Ask Cursor to analyze the time and space complexity of its own solution. This prompts the AI to identify inefficiencies and sets up the optimization request.
1// Follow-up prompt in the SAME chat session:2// What is the time and space complexity of this solution?3// Can you identify any performance bottlenecks for large4// input arrays (100,000+ elements)?Pro tip: Mentioning a specific input size (100,000+ elements) focuses Cursor on practical performance concerns rather than theoretical analysis.
Expected result: Cursor identifies the O(n^2) time complexity and suggests that a hash map approach would be O(n).
Request an optimized version
Request an optimized version
Ask for a faster version while keeping the same function signature and behavior. Specify the target complexity if you know it. Cursor will optimize while preserving the original API contract.
1// Follow-up prompt:2// Generate an optimized O(n) version using a hash map.3// Keep the same function signature and return type.4// Show both solutions side by side with their complexities.56// Cursor generates:7function findPairsOptimized(nums: number[], target: number): number[][] {8 const pairs: number[][] = [];9 const seen = new Map<number, number[]>(); // value -> indices10 for (let i = 0; i < nums.length; i++) {11 const complement = target - nums[i];12 if (seen.has(complement)) {13 for (const j of seen.get(complement)!) {14 pairs.push([j, i]);15 }16 }17 const existing = seen.get(nums[i]) || [];18 existing.push(i);19 seen.set(nums[i], existing);20 }21 return pairs;22}23// Time: O(n), Space: O(n)Expected result: An optimized O(n) solution alongside the original, with complexity comparison.
Request a memory-optimized variant
Request a memory-optimized variant
If the optimized solution uses too much memory, ask for a space-efficient alternative. Cursor can explore different tradeoffs when you specify the constraint.
1// Follow-up prompt:2// The hash map version uses O(n) extra space. Can you3// generate a version that uses O(1) extra space? It is4// acceptable to be O(n log n) time if it saves memory.5// Explain the tradeoff.Expected result: A space-efficient variant with a sort-based approach and clear explanation of the time-space tradeoff.
Apply the best solution with Composer
Apply the best solution with Composer
Once you have selected the best optimization, use Composer (Cmd+I) to apply it to your actual codebase. Reference the Chat conversation so Cursor applies the correct version.
1// Composer prompt (Cmd+I):2// @src/utils/arrayUtils.ts Replace the findPairs function3// with the O(n) hash map version from our chat discussion.4// Keep all existing exports and tests passing. Add a JSDoc5// comment noting the O(n) time and O(n) space complexity.Expected result: The optimized version replaces the original in your codebase with proper documentation.
Complete working example
1/**2 * Find all pairs of indices whose values sum to the target.3 * Time: O(n) | Space: O(n)4 *5 * @param nums - Array of numbers to search6 * @param target - Target sum to find pairs for7 * @returns Array of [index1, index2] pairs8 *9 * @example10 * findPairs([2, 7, 11, 15], 9) // [[0, 1]]11 * findPairs([3, 3, 3], 6) // [[0, 1], [0, 2], [1, 2]]12 */13export function findPairs(14 nums: number[],15 target: number16): [number, number][] {17 const pairs: [number, number][] = [];18 const seen = new Map<number, number[]>();1920 for (let i = 0; i < nums.length; i++) {21 const complement = target - nums[i];2223 if (seen.has(complement)) {24 for (const j of seen.get(complement)!) {25 pairs.push([j, i]);26 }27 }2829 const indices = seen.get(nums[i]);30 if (indices) {31 indices.push(i);32 } else {33 seen.set(nums[i], [i]);34 }35 }3637 return pairs;38}3940/**41 * Binary search helper for sorted array operations.42 * Time: O(log n) | Space: O(1)43 */44export function binarySearch(45 arr: number[],46 target: number,47 start = 0,48 end = arr.length - 149): number {50 while (start <= end) {51 const mid = Math.floor((start + end) / 2);52 if (arr[mid] === target) return mid;53 if (arr[mid] < target) start = mid + 1;54 else end = mid - 1;55 }56 return -1;57}Common mistakes when getting better optimizations from Cursor
Why it's a problem: Starting a new Chat session for each optimization attempt
How to avoid: Stay in the same Chat session. Use follow-up prompts to iterate. Cursor retains the full conversation context.
Why it's a problem: Asking for 'the most optimized version' without constraints
How to avoid: Specify your constraints: 'O(n) time, readable code, standard library only.' This produces practical optimizations.
Why it's a problem: Not testing the optimized version against the original
How to avoid: Ask Cursor to generate test cases that verify the optimized version produces identical output to the original.
Best practices
- Get a correct solution first, then optimize in follow-up prompts
- Ask for complexity analysis before requesting optimizations
- Specify target complexity (O(n), O(n log n)) when requesting alternatives
- Stay in the same Chat session to preserve context between iterations
- Request side-by-side comparison of original and optimized versions
- Include input size expectations so Cursor optimizes for your scale
- Document the chosen complexity in JSDoc comments for future maintainers
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
Write a function that finds all index pairs in an array whose values sum to a target. First write an O(n^2) brute force version, then optimize to O(n) using a hash map. Show both solutions with complexity analysis and explain the tradeoff.
In Cursor Chat (Cmd+L): @src/utils/arrayUtils.ts The findPairs function is O(n^2). Analyze its performance bottleneck, then rewrite it as O(n) using a hash map. Keep the same signature and return type. Add complexity comments.
Frequently asked questions
Does Cursor reliably identify complexity issues?
Cursor identifies common complexity patterns (nested loops, recursive calls) well. For subtle issues like amortized complexity or cache performance, be explicit in your prompt about what to analyze.
How many optimization iterations should I do in one session?
Two to three iterations work well. Beyond that, context gets polluted and Cursor may forget earlier constraints. Start a fresh session with a summary if you need more iterations.
Can Cursor benchmark the solutions?
Cursor can generate benchmark code but cannot run it. Ask it to create a benchmark script, then run it yourself in the terminal. Compare results to validate the optimization.
Should I use MAX mode for optimization work?
MAX mode provides deeper reasoning which helps with algorithmic optimization. It is worth the extra credits for performance-critical code paths.
Will Cursor optimize for my specific runtime (V8, Python CPython)?
Cursor optimizes algorithmically by default. If you need runtime-specific optimizations, mention the runtime: 'Optimize for V8 JavaScript engine' or 'Optimize for CPython with its GIL.'
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation