Performance measures how efficiently and responsively software operates under defined conditions. Code can pass functional tests at staging scale yet fail under production load: N+1 queries, poor algorithmic complexity, and excessive resource use may remain hidden until traffic grows. section 1.6
When expected traffic, throughput, and maximum latency are unknown, start with maintainable code rather than assuming a particular optimization will help. An optimization can add code, maintenance, or system complexity without improving the main workflow. Define the workload and measure the affected path before accepting that tradeoff. section 7.1
A parallel account search
An account finder filters a list by ID. Its sequential stream evaluates lazily and can stop when it finds a match. Replacing it with parallelStream() uses work-stealing to distribute tasks, but also introduces concurrency: processing should be stateless, and thread-pool utilization needs monitoring. Without knowing list size and workload, more threads are not evidence of a faster search. section 7.1.1
Listing 7.2 Initial filtering logic
private List<Account> accounts; #A
public AccountFinder(List<Account> accounts) {
this.accounts = accounts;
}
public Optional<Account> account(Integer id) {
return accounts.stream().filter(v -> v.getId().equals(id)).findAny();
}The baseline uses a sequential stream and findAny() to search the account list. section 7.1.1
Listing 7.5 Viewing the performance benchmark output
CH05.premature.AccountsFinderPerformanceBenchmark.baseline avgt 10 0.027 ± 0.002 ms/op CH05.premature.AccountsFinderPerformanceBenchmark.parallel avgt 10 0.030 ± 0.002 ms/op
In this benchmark, the baseline averaged 0.027 ms/op and the parallel version 0.030 ms/op. The result describes the measured workload, not every possible account search. section 7.1.1
AI-generated optimization deserves the same scrutiny. A request to make the account finder multithreaded produced several concurrent approaches and claims of performance gains. In a custom test with 10,000 accounts and 1,000 searches, the single-threaded approach took 25 ms, versus 63 ms for the parallel stream and substantially longer for the other concurrent approaches. Treat generated performance claims and tests as proposals to verify, not proof. section 7.1.2
Listing 7.7 The custom benchmark results
Single-threaded time: 25 ms Parallel stream time: 63 ms Concurrent time: 30398 ms Optimized time: 406 ms
The custom test reports elapsed times for the single-threaded, parallel, concurrent, and optimized approaches. section 7.1.2
Use performance tests to validate service-level objectives under relevant operating conditions. Keep an optimization when measured benefits justify its complexity, and revisit the test as workload assumptions change. section 1.6 section 7.1






















































