AVAILABLE ON VS CODE MARKETPLACE try it out at complexity lens1 on vs code

Inspiration As a CS student at CMU, I've spent countless hours debugging algorithms and optimizing code for projects like malloc lab and cache lab. The pain point? I never knew if my code was O(n) or O(n²) until I profiled it or got points docked. I wanted a tool that could tell me the complexity while I was writing the code, not after I submitted it. What it does ComplexityLens is a VSCode extension that analyzes Python code complexity in real-time:

Instant preview: Select any code and see ⚡ O(n²) appear inline within 500ms AI-powered analysis: Press Cmd+Shift+C for detailed breakdown including time complexity, space complexity, bottlenecks, and optimization suggestions Smart caching: Reuses results for identical code snippets to minimize API costs Fallback heuristics: Works offline with pattern-matching for common algorithms (binary search, merge sort, nested loops)

How we built it Tech stack:

TypeScript for VSCode extension API Claude Haiku (fast) for instant inline previews (~500ms response time) Claude Sonnet (accurate) for detailed full analysis Custom tokenizer for Python syntax highlighting

Architecture:

VSCode selection listener triggers on code highlight 400ms debounce prevents excessive API calls Cache lookup → API call → heuristic fallback (if API fails) Results displayed inline or in a styled webview panel

Key optimizations:

Used Claude Haiku instead of Sonnet for 3-5x faster previews Implemented LRU cache (max 50 entries) to avoid repeat API calls Minimal prompts ("Reply with ONLY Big-O notation") to reduce token costs

Challenges we ran into

Syntax highlighting hell: My first regex-based approach kept matching inside already-created HTML tags, breaking the display. Switched to a token-based parser that processes characters sequentially. Heuristic accuracy: Initial version counted sequential loops as nested (e.g., merge sort showed O(n³) instead of O(n log n)). Fixed by tracking indentation levels properly. TypeScript type safety: Had to handle undefined edge cases when managing the complexity cache with Map iterators. API cost management: Running Claude Sonnet on every selection would cost ~$1/hour of coding. Solution: Use Haiku for previews ($0.0003 each) and cache aggressively.

Accomplishments that we're proud of

Sub-second AI analysis: Most extensions with AI take 2-5 seconds. Ours shows results in <1 second. Actually useful: This solves a problem I've personally faced dozens of times while working on CMU systems projects. Production-ready caching: The extension is smart enough to not spam the API unnecessarily.

What we learned

VSCode extension API (decorations, webviews, configuration) Prompt engineering for minimal token usage while maintaining accuracy Balancing AI accuracy vs. speed (when to use Haiku vs. Sonnet) Python complexity analysis patterns (recognizing binary search, divide-and-conquer, etc.)

What's next for ComplexityLens

Support for more languages (C, JavaScript, Java) Integration with GitHub Copilot to suggest optimizations automatically Visualization of complexity growth curves (show how O(n²) scales vs. O(n log n)) Detection of common anti-patterns (e.g., item in list inside loops)

Built With

Share this project:

Updates