Performance patterns

From farming-labs/fm-skills · View interactive version

Performance patterns is an AI agent skill from the GitHub repository farming-labs/fm-skills. Explains web performance patterns including Core Web Vitals, network and JavaScript cost, rendering and hydration work, lazy loading, caching, and performance budgets. Use when diagnosing slow pages, improving user-perceived speed, or deciding which optimization strategy matters most.

How to use this skill

Skill file

Performance Patterns

Overview

Web performance is about how quickly users can:

Performance is not one problem. It is usually a combination of:

The right optimization depends on which of those is actually dominating.

The Main Performance Buckets

| Bucket | Typical Problem | Typical Fix |

|--------|------------------|-------------|

| Network | Large assets, slow server, too many requests | Compress, cache, preload, reduce bytes |

| JavaScript | Big bundles, expensive parsing, long tasks | Ship less JS, split code, defer work |

| Rendering | Large DOM, expensive layout/paint | Simplify UI work, reduce rerenders |

| Hydration | Too much client work before interactivity | Hydrate less, hydrate later, use islands |

| Data | Waterfalls, slow APIs, duplicate fetching | Parallelize, cache, prefetch, stream |

Core Web Vitals

Core Web Vitals are the most useful high-level user-centered performance signals.

| Metric | Measures | Good |

|--------|----------|------|

| LCP | How quickly main content becomes visible | <= 2.5s |

| INP | How quickly the page responds to interaction | <= 200ms |

| CLS | How stable the layout is while loading | <= 0.1 |

Interpreting Them

Do not optimize blindly. Start with the metric that is actually failing.

A Practical Performance Model

Think in this order:

Can the browser get the bytes quickly?
Can it understand and execute them cheaply?
Can it render useful content early?
Can the user interact without waiting?
Can the UI stay stable while it updates?

This usually maps to:

TTFB -> LCP -> Hydration / JS cost -> INP -> CLS

Performance Budgets

Budgets prevent performance from degrading gradually over time.

Common budgets:

Example budget mindset:

Home page:
- Initial JS under 180 KB compressed
- Hero image under 120 KB
- LCP under 2.5s on mid-tier mobile

Without budgets, performance work often becomes reactive and late.

Architecture Impacts Performance

| Pattern | Strength | Risk |

|---------|----------|------|

| CSR | Simple hosting, rich interactivity | Slow first render, high JS cost |

| SSR | Faster first content, better SEO | Server latency, hydration cost |

| SSG | Fastest repeatable loading for static content | Staleness, rebuild tradeoffs |

| Streaming | Early shell + progressive content | More complexity in boundaries |

| Islands / partial hydration | Less JS on page | More architecture complexity |

Architecture does not guarantee performance, but it changes where the performance risks move.

Pattern 1: Ship Less JavaScript

Large JS bundles hurt:

Common Approaches

// Load expensive feature only when needed
const Chart = lazy(() => import('./Chart'));

If you can delete JavaScript entirely, that is often better than optimizing it.

Pattern 2: Prioritize the Critical Path

The browser should receive the most important resources first.

Examples:

<link rel="preload" href="/hero.webp" as="image">
<script defer src="/app.js"></script>

Do not preload everything. Preloading too much just moves congestion around.

Pattern 3: Lazy Load Non-Critical Work

Load expensive code and assets when users actually need them.

Good candidates:

const Editor = lazy(() => import('./Editor'));

Lazy loading is useful only when the delayed resource is truly non-critical. If the user always needs it immediately, lazy loading may just add an extra wait.

Pattern 4: Reduce Render and Layout Work

Performance problems are not always network problems.

UI can be slow because:

Common Fixes

// Bad: expensive work during every render
const sorted = bigArray.sort(compareFn);

// Better: derive once per real input change
const sorted = deriveSortedItems(bigArray);

Pattern 5: Fetch Earlier, in Parallel, and Less Often

Data latency often dominates user experience.

Common mistakes:

Better patterns:

const [user, notifications] = await Promise.all([
  getUser(),
  getNotifications(),
]);

Pattern 6: Optimize Images and Media

Images are often the largest payload on a page and are frequent LCP candidates.

Rules of Thumb

<img
  src="/hero.webp"
  width="1200"
  height="800"
  alt="Product hero"
>

Missing dimensions are a common CLS bug.

Pattern 7: Manage Fonts Carefully

Fonts can block rendering or cause layout shifts.

Good practices:

Too many custom fonts can erase performance gains elsewhere.

Pattern 8: Control Third-Party Cost

Analytics, ads, chat widgets, A/B testing tools, and tag managers often create disproportionate performance damage.

Third-party scripts can hurt:

Rules of thumb:

Pattern 9: Defer Non-Urgent Work

Not all work must happen immediately after load or interaction.

Examples of deferrable work:

Split urgent work from non-urgent work whenever the platform or framework supports it.

Pattern 10: Stabilize the Layout

CLS is usually preventable.

Common causes:

Typical fixes:

Pattern 11: Cache the Right Things

Caching improves performance only when freshness is handled correctly.

Useful cache layers:

Caching helps performance when:

Caching hurts when stale data creates correctness bugs or forces wasteful refetch loops.

Pattern 12: Measure Before and After

Performance work without measurement turns into guesswork.

Useful Signal Types

| Type | Use |

|------|-----|

| Lab tests | Repeatable debugging in a controlled environment |

| Field data | Real-user experience in production |

| Bundle analysis | Detect JS growth |

| Profiler traces | Diagnose rendering and main-thread work |

Practical Workflow

  1. Identify the failing metric or slow user flow
  2. Find the dominant bottleneck
  3. Apply the narrowest meaningful fix
  4. Re-measure
  5. Add a budget or guardrail so it does not regress

Common Optimization Mistakes

| Mistake | Why It Fails |

|---------|--------------|

| Micro-optimizing components before checking bundle size | Big wins are often elsewhere |

| Lazy loading critical UI | User still waits, just later |

| Prefetching too aggressively | Wastes bandwidth and device resources |

| Caching everything | Creates correctness and invalidation issues |

| Measuring only on a fast laptop | Hides real user pain |

| Chasing one benchmark while hurting UX elsewhere | Metrics are proxies, not the product |

Decision Guide

Use this rough mapping:

Rules of Thumb

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.