# What are the top performance optimization best practices for enterprise-grade LWC applications?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Performance Optimization, Best Practices

Read it on the site: https://www.forcechallenges.com/interview-questions/what-are-the-top-performance-optimization-best-practices-for-enterprise-grade-lwc-applications

## Answer

**Basics:** Slow components usually suffer from too many server calls, huge lists in the DOM, expensive work in render paths, or unnecessary re-renders. Measure first with the browser Performance tab, the Lightning Usage and Salesforce Lightning performance tools (Lightning Inspector-style tooling, EPT in LEX), and the Network tab.

**How it works:** Data: use `@wire` with cacheable Apex or LDS adapters so results are cached and shared; request only needed fields; avoid duplicate calls across sibling components by sharing through a parent or service module; debounce typed input; paginate or use infinite loading. Rendering: use `lwc:if` to avoid rendering hidden content, keep template depth small, provide a stable unique `key` in `for:each`, and load heavy parts lazily with dynamic `import()` when a feature is opened.

**Advanced / deep dive:** Avoid expensive getters (a getter runs on every render and should be cheap, so precompute in JS and store). Do not mutate reactive state inside `renderedCallback` without a guard, as it can loop. Batch state changes so the engine renders once per microtask. Prefer parent-to-child via `@api` and events upward over broad `window` listeners and DOM queries. Reduce payload size: select specific fields, add `LIMIT`, and avoid returning full SObjects when a DTO with a few fields will do. For large datasets use server-side filtering and `lightning-datatable` with infinite loading rather than rendering thousands of rows. Use `lightning-*` base components, which are optimized and accessible, over custom lookalikes. Cache static or expensive derived data in a shared JS service module. Watch component count: dozens of tiny components per row multiply cost. Minify and zip third-party libraries and load only when needed. Check Apex side too: cacheable methods, selective queries and indexes dominate perceived speed.

**Code / config example:**
```javascript
// shared service: one cached lookup reused by many components
let cache;
export async function getPicklist(loader) {
    if (!cache) cache = loader();   // store the promise, not the result
    return cache;
}
// lazy load a heavy feature only when needed
async handleOpenEditor() {
    const { default: Editor } = await import('c/heavyEditor');
    this.editorCtor = Editor;
}
```

**Hidden gotchas & must-remember points:**
- Missing or index-based `key` values cause extra DOM churn and wrong state on reordering.
- Getters run every render; keep them cheap and pure.
- `cacheable=true` Apex is client-cached; non-cacheable calls are always fresh and slower.
- Event listeners on `window` leak without `disconnectedCallback` cleanup.
- Rendering 1,000+ rows is slow; paginate.
- Always profile before optimizing.

**Best practice:** Fetch minimal data through cached wires, render only visible UI, keep templates shallow, and measure with browser and Salesforce performance tools.

---
Source: ForceChallenges · last updated 2026-11-02
