Skip to content

Performance Anti-Patterns

Learning Objectives

  • Identify frequent performance anti-patterns.
  • Replace inefficient patterns with practical alternatives.
  • Measure improvements objectively.

Common Anti-Patterns

  • Nested loops for repeated lookups.
  • Recomputing same expensive values inside loops.
  • Excessive object creation in hot code paths.
  • Using unsuitable data structures for access pattern.

Better Alternatives

  • Use hash-based lookup for membership checks.
  • Cache repeated computation results.
  • Batch operations where possible.
  • Move constant work outside loops.

Practice Tasks

  1. Find one O(n^2) routine and optimize it.
  2. Benchmark before and after optimization.
  3. Document runtime and memory trade-offs.

Summary

Optimize based on bottlenecks, not assumptions.