Forms, Flows and Production · Lesson 23 of 25
LWC Performance and Security Best Practices
A component that works in a developer org with ten records can crawl in production with ten thousand, or expose data it shouldn't. This lesson collects the habits that separate production-grade LWC from demo code: load less, load it once, render efficiently, and keep security on the server where it belongs.
In this lesson
- Loading data efficiently
- Rendering efficiently
- Lightning Web Security
- A pre-release checklist
Load less, load once
| Practice | Why |
|---|---|
Prefer LDS (getRecord, record forms) when it can do the job | shared cache across components; no duplicate server calls; automatic sync |
Mark read-only Apex cacheable=true | results are cached on the client and reused |
Query only the fields you display, with a LIMIT | smaller payloads, faster SOQL, fewer governor-limit surprises |
| Paginate or use infinite loading for lists | never render thousands of rows at once |
| Combine related queries into one Apex call | each server round trip costs time, especially on mobile |
| Debounce input-driven calls | one request after the user pauses, not one per keystroke |
Render efficiently
- Keep getters cheap. They run on every render. Precompute expensive values when data arrives (in the wire handler) and store them in a field.
- Use stable keys in lists so LWC updates rows instead of recreating them.
- Don't render what isn't visible:
lwc:iffor closed sections, lazy tabs, and pagination. - Lazy-load heavy code: third-party libraries via
loadScriptonly when needed; rarely used child components with dynamic import (lwc:componentandlwc:is). - Avoid render loops: never set a reactive field unconditionally in
renderedCallback.
// precompute once when data arrives, instead of in a getter that runs on every render
@wire(getOrders, { accountId: '$recordId' })
wiredOrders({ data, error }) {
if (data) {
this.rows = data.map((o) => ({
...o,
totalLabel: currency.format(o.Total__c),
isLate: new Date(o.Due_Date__c) < this.today,
}));
this.lateCount = this.rows.filter((r) => r.isLate).length;
} else if (error) {
this.error = error;
}
}
Lightning Web Security
Lightning Web Security (LWS) is the security architecture that runs your components (it replaced Lightning Locker). Each namespace gets its own sandboxed JavaScript environment, so one package's code can't read or tamper with another's, and dangerous browser APIs are restricted. Practical consequences:
- Your component can only reach DOM elements it owns – use
this.templateandlwc:ref, notdocument.querySelector. - Third-party libraries load as static resources with
loadScript/loadStyle; external scripts from CDNs are blocked by the Content Security Policy unless trusted. - Callouts from the browser to other domains need CSP Trusted Sites; most integrations should go through Apex instead.
- Never put user input into
innerHTMLorlwc:dom="manual"without sanitising – uselightning-formatted-rich-textfor rich text.
The browser is not a security boundary
Hiding a button or a field in LWC only changes the UI. Every @AuraEnabled method can be called directly by a user with access to its class. Enforce sharing (with sharing), object and field permissions (WITH USER_MODE, as user DML) and business permissions (custom permissions, explicit checks) in Apex.
Pre-release checklist
- Loading, error and empty states are handled; errors show helpful messages.
- Apex is
with sharing, usesUSER_MODE, bind variables andLIMIT; cacheable where read-only. - Works with realistic data volumes and on mobile; no console errors.
- Accessible: labels on every input, keyboard navigation,
alternative-texton icons and spinners. - Jest tests cover the main paths (next chapter), and ESLint passes with the LWC rules.
- js-meta.xml exposes only the targets and properties admins need.
Practice
Try it yourself
A getter sorts and formats 2,000 rows and is used in the template. What's the problem, and how do you fix it?
Show answer
The getter runs on every render – typing in any field re-sorts 2,000 rows. Do the sort and formatting once when the data arrives (in the wire handler or after the imperative call), store the result in a field, and render a page of rows at a time.
Your Apex method returns all fields of Contact with
without sharing"to avoid permission errors". What would you change?Show answer
Use
with sharingandWITH USER_MODE, select only the fields the component shows, add a LIMIT, and grant users the permissions they genuinely need instead of bypassing security.
Quick check
Why prefer LDS over Apex for single records?
Shared client cache, automatic sync between components and built-in security.
What replaced Lightning Locker?
Lightning Web Security (LWS).
Can hiding a button in LWC stop a user from running the Apex behind it?
No – enforce permissions in Apex.
Summary
- Load less and load once: LDS, cacheable Apex, selective queries, pagination, debouncing.
- Render efficiently: cheap getters, stable keys, lazy loading, no render loops.
- LWS sandboxes components; real security lives in Apex.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.