Prompt
In performance-sensitive code paths, minimize unnecessary work by ordering conditions to short-circuit early, and avoid redundant async wrappers.
Apply these rules:
- Guard with cheap predicates first (e.g.,
enabled/boolean flags) so later conditions/callbacks are never evaluated when they can’t change the outcome. - Don’t call expensive checkers unless needed: ensure threshold/count/boolean conditions gate any
retryChecker(...)-style functions. - In
asyncfunctions, return values directly—don’t wrap them withPromise.resolve, and avoid unnecessaryelseafter areturn.
Example patterns:
// 1) Guard before evaluating other conditions
const shouldRefetch = query.instances.some(instance =>
instance.config.enabled && instance.config.refetchIntervalInBackground
);
// 2) Gate expensive retryChecker with cheaper conditions
const canRetryByCount = query.state.failureCount <= query.config.retry;
const shouldRetry =
query.config.retry === true ||
(canRetryByCount && query.config.retryChecker(error, query.state.failureCount));
// 3) Avoid redundant Promise.resolve in async functions
async function getData() {
// ...
return query.state.data; // no Promise.resolve, no redundant else
}
This reduces wasted evaluations and prevents unnecessary function calls, improving execution speed and resource utilization.