← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · ✎ updated 7d
← Back to glossary

Lazy Loading

Performance HTML5 Beginner
debt(d5/e3/b5/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

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.

e3 Effort Remediation debt — work required to fix once spotted

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.

b5 Burden Structural debt — long-term weight of choosing wrong

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.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

Also Known As

lazy load deferred loading on-demand loading

TL;DR

Defer loading or initialising a resource until it is actually needed, reducing startup cost and memory usage.

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

✗ Lazy loading always improves performance. Lazy loading defers work until needed — beneficial for optional heavy resources, but catastrophic for ORM relationships in loops where it triggers the N+1 problem: one query per row instead of one query for all rows.

Why It Matters

Lazy loading defers expensive initialisation until the moment it is actually needed — objects that are never used cost nothing to create. In frameworks, lazy loading service containers means only the services actually touched by a request are instantiated.

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

✗ Vulnerable
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);
    }
}
✓ Fixed
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;
    }
}

Added 13 Mar 2026
Edited 22 Mar 2026
Views 171
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 0 pings S
No pings yet today
No pings yesterday
PetalBot 16 Amazonbot 14 Ahrefs 13 Perplexity 9 SEMrush 9 Scrapy 9 Bing 7 Google 6 Unknown AI 4 Brave Search 3 Majestic 2 Twitter/X 2 Applebot 2 Meta AI 1 ChatGPT 1 Baidu 1
crawler 96 crawler_json 2 your_contextpost 1
⚡ DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Add loading='lazy' to all below-the-fold images and iframes — PHP renders this attribute; the browser defers loading until the image approaches the viewport
📦 Applies To
any HTML5 web
🔗 Prerequisites
🔍 Detection Hints
<img> without loading=lazy; all images loaded eagerly including below-fold; no IntersectionObserver for deferred content
Auto-detectable: ✓ Yes lighthouse pagespeed-insights
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update


✓ schema.org compliant