Genie React

AI React performance tuning: an agent cut a 26 ms render to 0.02 ms

Typing a 27-character note re-rendered a product picker 27 times at about 26 ms each. Genie named the component and its cost, and verify proved the fix.

Typing a 27-character note on a New order page re-rendered ProductPicker 27 times, and each render spent 25.9 ms in the picker's own code. A coding agent doing AI React performance work with Genie traced that cost to a search index rebuilt on every render, moved the build into useMemo, and measured 0.02 ms per render afterwards.

The run uses apps/realworld-dashboard, a Vite and React 19 app in this repo. Every number comes from one run of Genie 1.1.0 against a React development bundle on 2026-11-19. Production builds do less work per render, so read the numbers as before and after, not as what users see. The raw evidence has the full output.

Typing a note re-rendered the product picker 27 times

The symptom is lag in the Notes for the warehouse team field. Open a render window, type a sentence with agent-browser, wait for the app to settle, and rank what rendered by self time:

node_modules/.bin/genie-react call react_clear_renders '{"component":"ProductPicker"}' --session upd-f
agent-browser --session upd-f type 'textarea[name="notes"]' 'Leave at dock 4, call ahead'
node_modules/.bin/genie-react call devtools_wait \
  '{"condition":"settled","domains":["react","query","router"],"timeoutMs":10000}' --fail-on-result-error --session upd-f
node_modules/.bin/genie-react call react_get_renders '{"sort":"selfTime","limit":5}' --session upd-f

Trimmed to the bundle and the top two rows:

bundle: development
commits: 27
components[5] (name #id {fields}, then ← kind evidence name {fields} per cause):
  each: {renders:27,updates:27,latestDocumentCommitId:36,inputEvidence:changed,source:{ownership:app}}
  ProductPicker #5267106300044 {…,selfTime:27.1,totalTime:27.1,cumulativeSelfTime:698.5,cumulativeTotalTime:699,causeCounts:{props:54,parent:27},necessity:unknown,necessityReason:callbacks-changed,source:{location:/src/features/orders/create-order-page.tsx:172:15,role:element-callsite}}
    ← parent inferred CardBody {origin:CreateOrderPage}
    ← props exact selectedIds {referenceOnly:true}
    ← props exact onPick
  CreateOrderPage #5267106299957 {…,noObservedInputChange:27,selfTime:0.8,totalTime:28.4,cumulativeSelfTime:14,…,causeCounts:{state:27},necessity:unnecessary,necessityReason:inputs-unchanged,…}
    ← state exact "state[3]"

27 characters produced 27 commits, and ProductPicker rendered in every one. Its cumulative self time was 698.5 ms, or 25.9 ms per render.

Genie names the component, the trigger, and the cost

Self time is the time React spent in a component's own function, without its children. That narrows the search to one function body:

src/features/orders/product-picker.tsx
const { formatMoney } = usePreferences()
const [term, setTerm] = useState('')
const index = buildProductIndex(products)
const matches = term.trim().length < MIN_TERM_LENGTH ? [] : index.search(term, RESULT_LIMIT)

The search term was empty, so index.search never ran. The work is buildProductIndex, which builds trigram and typo-tolerant postings over all 2,400 products on every render.

The causes explain why the picker rendered at all. CreateOrderPage re-rendered 27 times from a state hook, state[3]. The page calls form.watch() with no arguments, which re-renders it when any field changes. The picker then received a new selectedIds array with equal contents (referenceOnly:true) and a new onPick function.

Genie points at the component and its cost. It does not name the slow line. The agent read a four-line body to find it. In a long component, you would need the browser's Performance panel for that step.

Build the index once, then stop the extra renders

The first change builds the index only when products changes:

src/features/orders/product-picker.tsx
-import { useState } from 'react'
+import { useMemo, useState } from 'react'
@@
-  const index = buildProductIndex(products)
+  const index = useMemo(() => buildProductIndex(products), [products])

The same typing and the same read on the picker:

node_modules/.bin/genie-react call react_get_renders '{"component":"ProductPicker","appOnly":false}' --session upd-f --fields bundle,components,coverage

Before the change, from the same window as the ranking above, trimmed:

{bundle:development,components:[{…,name:ProductPicker,renders:27,updates:27,selfTime:27.1,totalTime:27.1,cumulativeSelfTime:698.5,…}],coverage:{…,complete:true,…}}

After:

{bundle:development,components:[{…,name:ProductPicker,renders:27,updates:27,selfTime:0.1,totalTime:0.1,cumulativeSelfTime:0.6,…}],coverage:{…,complete:true,…}}

0.6 ms over 27 renders is 0.02 ms per render. The picker still rendered 27 times, though. Each render was cheap, but the picker shows nothing that depends on the notes.

The second change stops notes keystrokes from reaching the picker. Wrap it in memo and give it props that keep their identity:

src/features/orders/product-picker.tsx
-import { useMemo, useState } from 'react'
+import { memo, useMemo, useState } from 'react'
@@
-export function ProductPicker({
+export const ProductPicker = memo(function ProductPicker({
@@
-}
+})
src/features/orders/create-order-page.tsx
+import { useCallback, useMemo } from 'react'
@@
-import { ORDER_PRIORITIES, type OrderPriority } from '../../api/types'
+import { ORDER_PRIORITIES, type OrderPriority, type Product } from '../../api/types'
@@
   const values = form.watch()
+  const appendLine = lines.append
+  const selectedIds = useMemo(() => lines.fields.map((line) => line.productId), [lines.fields])
+  const addProduct = useCallback(
+    (product: Product) =>
+      appendLine({
+        productId: product.id,
+        name: product.name,
+        unitPrice: product.unitPrice,
+        quantity: 1,
+      }),
+    [appendLine],
+  )
@@
-                selectedIds={values.lines.map((line) => line.productId)}
-                onPick={(product) =>
-                  lines.append({
-                    productId: product.id,
-                    name: product.name,
-                    unitPrice: product.unitPrice,
-                    quantity: 1,
-                  })
-                }
+                selectedIds={selectedIds}
+                onPick={addProduct}

lines.fields comes from useFieldArray and changes only when a line is added or removed. After the change, agent-browser picked Pallet Wrap XL, the row appeared in the order table, and the same match showed as disabled in the next search. The picker still works.

Verify that notes keystrokes skip the picker

verify claims cover render counts, render causes, effects and error boundaries. They do not cover time. So the claims check work you can count. The picker must render 0 times while you type. The page must render at least once, which proves the typing reached the app. No error boundary may catch. Empty the field with agent-browser fill 'textarea[name="notes"]' '', then run:

node_modules/.bin/genie-react verify begin --session upd-f --name 'type warehouse notes' \
  --claim 'render ProductPicker max 0' --claim 'render CreateOrderPage min 1' --claim 'boundaries clean'
agent-browser --session upd-f type 'textarea[name="notes"]' 'Leave at dock 4, call ahead'
node_modules/.bin/genie-react verify end vfy_d19d4b18-bdbc-4c60-af4d-bb914300d9ee --session upd-f

On the original code, trimmed to the verdict and claim lines, it exits 1:

fail vfy_d19d4b18-bdbc-4c60-af4d-bb914300d9ee "type warehouse notes"  27 commits, quiet after 714 ms (react, query, router, network)
  1 fail render ProductPicker max 0  {renders:27,mounts:0,updates:27,remounts:0,semantics:exact,instances:1,activeInstances:1}
  2 pass render CreateOrderPage min 1  {renders:27,mounts:0,updates:27,remounts:0,semantics:exact,instances:1,activeInstances:1}
  3 pass boundaries clean  {caughtDuringWindow:0,caughtAtEnd:0}

With both changes applied, the same claims and typing exit 0:

pass vfy_cf41b9e8-50da-48da-a5d0-631dfbdfad64 "type warehouse notes"  27 commits, quiet after 744 ms (react, query, router, network)
  1 pass render ProductPicker max 0  {renders:0,mounts:0,updates:0,remounts:0,semantics:exact,instances:1,activeInstances:1}
  2 pass render CreateOrderPage min 1  {renders:27,mounts:0,updates:27,remounts:0,semantics:exact,instances:1,activeInstances:1}
  3 pass boundaries clean  {caughtDuringWindow:0,caughtAtEnd:0}
attempt 2 for these claims on this tab; previous fail vfy_d19d4b18-bdbc-4c60-af4d-bb914300d9ee; document reloaded since

With useMemo alone, the picker still rendered 27 times, so claim 1 needs both changes. After the run, agent-browser read the field back as Leave at dock 4, call ahead.

Six runs on each side hold the timing

One run can be noise. In the same dev server session, the agent captured six runs of the same typing before the useMemo change and six after, then compared them with a 100 ms budget:

node_modules/.bin/genie-react call devtools_capture_compare '{"baselineCaptureIds":[…6 before…],"candidateCaptureIds":[…6 after…],"metrics":["react.selfTimeMs","react.renders"],"budgets":[{"metric":"react.selfTimeMs","maxValue":100}]}' --fail-on-result-error --session upd-f

Trimmed, it exits 0:

overall: pass
  react.selfTimeMs pass {…,baseline:{samples:5,median:749.8,p95:893.8,…},candidate:{samples:5,median:67.3,p95:73.74,…},comparable:true,…,budget:{metric:react.selfTimeMs,maxValue:100},confidence:{…,pValue:0.0079,significant:true,…}}
  react.renders informational {…,baseline:{samples:5,median:729,…},candidate:{samples:5,median:729,…},…}

Genie drops the first capture of each side as warm-up. react.selfTimeMs totals the self time of every render in the capture's window, across all 729 renders of the sentence. The median fell from 749.8 ms to 67.3 ms, 91% less. The picker alone took 698.5 ms in the first read, so it makes up most of the before total. The 67.3 ms left is the rest of the page, which still renders as often as before. Genie 1.0.2 added up one render per component, about one keystroke, and 1.1.0 fixed that.

Size the budget for the whole interaction. The same comparison with "maxValue":8 fails with candidate median 67.3 exceeds maxValue 8, although the fix worked.

Swap the two sides and the 100 ms budget fails with candidate median 749.8 exceeds maxValue 100. With --fail-on-result-error, that call exits 1, so a CI step can gate on it. Without the flag, it exits 0 and prints overall: fail.

When this approach does not fit

  • Genie times renders in a development bundle. These numbers show the direction and size of a change, not production latency. Measure a production profiling build before you quote user-facing numbers.
  • verify has no timing claim. A fix that only makes renders cheaper, like the useMemo change alone, needs devtools_capture_compare to prove it.
  • Self time stops at the component. When the body is long, profile it in the browser to find the slow call.
  • The useMemo holds while products keeps its identity. Here it is useQuery data. This walkthrough did not test a refetch.

Optimize a slow interaction covers the measuring loop, and Verify a fix lists every claim form.

On this page