Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/async-cheap-condition-before-await.md
1---2title: Check Cheap Conditions Before Async Flags3impact: HIGH4impactDescription: avoids unnecessary async work when a synchronous guard already fails5tags: async, await, feature-flags, short-circuit, conditional6---78## Check Cheap Conditions Before Async Flags910When a branch uses `await` for a flag or remote value and also requires a **cheap synchronous** condition (local props, request metadata, already-loaded state), evaluate the cheap condition **first**. Otherwise you pay for the async call even when the compound condition can never be true.1112This is a specialization of [Defer Await Until Needed](./async-defer-await.md) for `flag && cheapCondition` style checks.1314**Incorrect:**1516```typescript17const someFlag = await getFlag()1819if (someFlag && someCondition) {20// ...21}22```2324**Correct:**2526```typescript27if (someCondition) {28const someFlag = await getFlag()29if (someFlag) {30// ...31}32}33```3435This matters when `getFlag` hits the network, a feature-flag service, or `React.cache` / DB work: skipping it when `someCondition` is false removes that cost on the cold path.3637Keep the original order if `someCondition` is expensive, depends on the flag, or you must run side effects in a fixed order.38