Algorithmic Improvements
vs. Micro-Optimizations
Learn when to rethink your approach entirely versus when to fine-tune existing code: the single most important judgment call in software performance.
What is Performance Optimization?
Performance optimization means making your code run faster or use fewer resources. But not all optimizations are created equal: some deliver massive speedups, while others barely make a difference.
Quick Refresher: Big-O Notation
Big-O describes how execution time grows as the input size \( n \) increases. It tells us the worst-case scaling behavior of an algorithm.
Why This Distinction Matters
Imagine searching for a name in a phone book with 1,000,000 entries. Compare two strategies:
The Core Principle
Algorithmic vs Micro: Key Differences
| Algorithmic Improvement | Micro-Optimization | |
|---|---|---|
| What changes | Algorithm or data structure | Code-level implementation details |
| Big-O impact | Changes the complexity class | Same complexity class |
| Typical speedup | 10x to 1,000,000x | 1.05x to 3x |
| When it matters | Large or growing datasets | Hot loops in already-optimized code |
| Effort | Higher: requires rethinking approach | Lower: small, local code changes |
| Example | Replace nested loop with a Set | Use ++i instead of i++ |
How to Decide Which Approach
console.time().Example 1: Finding Duplicates
Given an array of \( n \) numbers, find if any value appears more than once.
The Algorithmic Fix: Use a Set
Example 2: Sorting 10,000 Items
You need to sort an array. Consider two different sorting algorithms:
When Micro-Optimization Is Right
Your algorithm is already \( O(n) \). The profiler shows a specific line runs millions of times per frame in a game loop. Now micro-optimization makes sense:
The Decision Flowchart
Real-World Analogy
Imagine you need to deliver packages from New York to Los Angeles:
Examples in JavaScript
| Scenario | Algorithmic Fix | Micro-Optimization |
|---|---|---|
| Find item in list | Sort once, then use binary search for repeated lookups | Use === over == |
| Remove duplicates | Use a Set | Cache .length in loop |
| Count frequencies | Use a Map | Pre-allocate object keys |
| Build strings | Use Array.join() | Use template literals |
| Shortest path | Dijkstra over brute force | Use typed arrays for nodes |
Common Mistakes
Key Takeaways
Question 1 of 5
A function uses two nested loops to check all pairs in an array, resulting in \( O(n^2) \) time. A developer replaces the inner loop with a Set lookup, reducing it to \( O(n) \). This is an example of:
Question 2 of 5
A developer changes for (let i = 0; i < arr.length; i++) to const len = arr.length; for (let i = 0; i < len; i++). What type of optimization is this?
.length avoids re-reading the property each iteration, but the loop still visits every element once: it remains \( O(n) \). This is a classic micro-optimization: a small constant-factor improvement within the same complexity class.Question 3 of 5
Your app is slow. You suspect the rendering function is the bottleneck but have not measured. What should you do first?
Question 4 of 5
An \( O(n^2) \) sorting algorithm is micro-optimized to run 2x faster. For \( n = 100{,}000 \), approximately how many operations does it perform compared to an \( O(n \log n) \) algorithm?
Question 5 of 5
You have an \( O(n) \) function that runs inside a game loop at 60fps. Profiling shows it allocates many temporary objects. Which optimization strategy is most appropriate?