A developer is building a Lightning Web Component for a Lightning Experience app. The component contains a form with an <input> for a search term and a list of results. When the user types into the input, the component must call an Apex method that returns matching records. The developer observes that the Apex method is being invoked on every keystroke, causing excessive server calls. Which technique should the developer use to limit the number of Apex calls while still updating the results as the user types?
Trap 1: Set the Apex method to cacheable=true and call it imperatively from…
Caching with cacheable=true improves performance only when the same parameters are used repeatedly. In this scenario, the search term changes with each keystroke, so the cache will miss and the method will still be called each time. It does not reduce the frequency of calls.
Trap 2: Use the lightning-input component with the onchange event and call…
Checking length does not prevent calls on every keystroke after the third character. It only skips very short terms, but the user will still trigger many calls while typing a longer word. This does not effectively limit the number of Apex calls to a manageable rate.
Trap 3: Use the @wire decorator with the Apex method and pass the search…
The @wire decorator does not provide debouncing or throttling. It will call the Apex method whenever the reactive parameter changes, which occurs on every keystroke. This does not solve the excessive server calls problem and may even worsen it by automatically invoking the method without control over timing.
- A
Set the Apex method to cacheable=true and call it imperatively from the component's JavaScript.
Why it fails: Caching with cacheable=true improves performance only when the same parameters are used repeatedly. In this scenario, the search term changes with each keystroke, so the cache will miss and the method will still be called each time. It does not reduce the frequency of calls.
- B
Implement a debounce function using setTimeout and clearTimeout to delay the Apex call until the user pauses typing.
Debouncing delays the Apex call until a period of inactivity has passed after the last keystroke. This reduces the number of calls to one per pause, which is the standard technique for limiting server calls in LWC. It is the correct approach because it controls the timing of imperative Apex calls.
- C
Use the lightning-input component with the onchange event and call the Apex method only when the input value length exceeds three characters.
Why it fails: Checking length does not prevent calls on every keystroke after the third character. It only skips very short terms, but the user will still trigger many calls while typing a longer word. This does not effectively limit the number of Apex calls to a manageable rate.
- D
Use the @wire decorator with the Apex method and pass the search term as a reactive parameter.
Why it fails: The @wire decorator does not provide debouncing or throttling. It will call the Apex method whenever the reactive parameter changes, which occurs on every keystroke. This does not solve the excessive server calls problem and may even worsen it by automatically invoking the method without control over timing.