Lazy Loading
debt(d5/e3/b5/t7)
Closest to 'specialist tool catches it' (d5). The detection_hints list Lighthouse and PageSpeed Insights — specialist performance auditing tools — as the means to catch missing lazy loading on images. The code pattern (images without loading=lazy, no IntersectionObserver) is not caught by a compiler or default linter, requiring deliberate profiling or audit tooling to surface.
Closest to 'simple parameterised fix' (e3). The quick_fix describes adding loading='lazy' to below-the-fold images and iframes — a small, mechanical change. However, fixing the ORM N+1 variant (lazy loading inside loops) requires identifying affected query paths and switching to eager loading, which is slightly more than a one-liner but still contained within one component, landing at e3.
Closest to 'persistent productivity tax' (b5). The applies_to scope is 'web' broadly, and tags span frontend, database, and OOP — meaning the pattern affects multiple work streams. The N+1 footgun in ORM usage means every developer working with database-backed loops must keep this pattern in mind, imposing an ongoing productivity tax across many features without being fully architectural.
Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception field directly describes this: the 'obvious' belief is that lazy loading always improves performance, but in ORM loops it triggers the N+1 problem — the opposite of what a developer expects. This contradicts the intuition from the image/resource loading context where deferral is unambiguously beneficial, making it a serious cross-context trap.
Also Known As
TL;DR
Explanation
Lazy loading defers expensive operations (database queries, object construction, file reading) until the result is actually needed. In ORMs, it loads related objects on first access rather than eagerly with every query. In PHP DI containers, services are constructed on first request rather than at container build time. Lazy loading reduces initial response time and memory pressure for requests that don't need a resource. The trade-off is that it can hide N+1 query problems — consider eager loading for known-required relationships.
Common Misconception
Why It Matters
Common Mistakes
- Lazy-loading inside a loop — the deferred cost is still paid, just spread across iterations.
- Forgetting that lazy loading adds a conditional check on every access — negligible for expensive resources, wasteful for cheap ones.
- Confusing lazy loading (defer creation) with caching (reuse result) — they solve different problems.
- Using lazy loading to paper over a design problem — if an object is expensive to create, that is worth fixing directly.
Code Examples
class UserProfile {
public function __construct(private int $userId) {
// Loads posts immediately even if caller only needs the avatar
$this->posts = $this->db->fetchPosts($userId);
$this->comments = $this->db->fetchComments($userId);
}
}
class UserProfile {
private ?array $posts = null;
private ?array $comments = null;
public function __construct(private int $userId) {}
public function getPosts(): array {
$this->posts ??= $this->db->fetchPosts($this->userId); // load on first access
return $this->posts;
}
public function getComments(): array {
$this->comments ??= $this->db->fetchComments($this->userId);
return $this->comments;
}
}