ALWAYS measure before optimizing. Establish a quantitative baseline (latency percentiles, throughput, memory usage, CPU utilization) before making any change. Optimization without measurement is guessing.
NEVER optimize without a reproducible benchmark. Every optimization claim must be verified by a benchmark that can be re-run to confirm the improvement and detect future regressions.
ALWAYS change one variable at a time. If you optimize the query and add caching simultaneously, you cannot attribute the improvement to either change. Isolate variables for clear attribution.
NEVER rely on average latency as your primary metric. Use percentiles: p50 (median), p95, and p99. Averages hide outliers that affect real users. A system with 10ms average latency might have 5-second p99 latency.
ALWAYS consider the entire request path, not just the code. A function that runs in 1ms is not the bottleneck if it waits 200ms for a database round trip. Profile the full request lifecycle: network, load balancer, application, database, serialization, and network return.
NEVER optimize code that is not on the hot path. If a function runs once at startup, making it 10x faster saves milliseconds total. Focus optimization effort on code that runs thousands or millions of times.
ALWAYS account for warm cache versus cold cache in benchmarks. Report both scenarios because they represent different user experiences: first visit (cold) versus subsequent visits (warm).
NEVER assume more hardware solves the problem. Throwing resources at an O(n squared) algorithm delays the problem instead of solving it. Fix algorithmic complexity before scaling horizontally.
ALWAYS test with realistic data volumes. A query that runs in 1ms on 100 rows may take 30 seconds on 10 million rows. Performance tests must use data volumes that match production.
NEVER ignore memory pressure. High memory usage causes garbage collection pauses, swap thrashing, and out-of-memory crashes. Monitor heap usage, allocation rate, and GC pause duration alongside latency.
ALWAYS document the methodology, environment, and results of every performance test. Undocumented optimizations cannot be verified, reproduced, or maintained.
NEVER ship a performance optimization without verifying it does not change behavior. Run the full test suite after every optimization. A faster function that returns wrong results is not an optimization.