Files
ghbot/.agents/skills/zod/references/perf-cache-schemas.md
T
ghbot 78bf8e9e41 feat: v2.0 iteration — test coverage, reliability, observability
M0: Test coverage expansion (151 → 321 tests, 61% → 66% lines)
  - normalizeKnowledge CRLF/LF normalization fix
  - gooseReviewer: 11 tests, 97.35% coverage
  - r2 storage: 22 tests, 100% coverage
  - triage processor: 11 tests, exported buildTriagePrompt/labelName
  - webhook processor: 35 tests, 72.39% coverage
  - conflict resolver: 12 tests
  - review processor: 7 tests

M1: Reliability hardening
  - Smart retry with jitter, maxTotalTimeoutMs, custom retryable predicate
  - MergeGuard: in-memory merge dedup tracker (100% coverage)
  - failureMessages: 7-category failure classifier (100% coverage)

M2: Observability
  - MetricsCollector: Actions-run metrics with snapshot/markdown/reset
  - 11 tests, 100% coverage

New files: 12 test files, 3 source modules
All gates: typecheck ✓ lint ✓ format ✓ 321/321 tests ✓ build ✓
2026-08-24 17:34:04 +08:00

144 lines
3.1 KiB
Markdown

---
title: Cache Schema Instances
impact: LOW-MEDIUM
impactDescription: Creating schemas on every render/call wastes CPU; module-level or memoized schemas are created once
tags: perf, cache, memoization, optimization
---
## Cache Schema Instances
Schema creation has overhead. Creating schemas inside render functions or on every function call wastes CPU cycles. Define schemas at module level or memoize them so they're created once and reused.
**Incorrect (creating schema every render):**
```typescript
import { z } from 'zod'
function UserForm() {
// Schema created on EVERY render - wasteful
const userSchema = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().int().positive(),
})
const handleSubmit = (data: unknown) => {
const result = userSchema.safeParse(data)
// ...
}
return <form onSubmit={handleSubmit}>...</form>
}
```
**Correct (module-level schema):**
```typescript
import { z } from 'zod'
// Schema created ONCE at module load
const userSchema = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().int().positive(),
})
type User = z.infer<typeof userSchema>
function UserForm() {
const handleSubmit = (data: unknown) => {
const result = userSchema.safeParse(data)
// ...
}
return <form onSubmit={handleSubmit}>...</form>
}
```
**For dynamic schemas, use useMemo:**
```typescript
import { z } from "zod";
import { useMemo } from "react";
function DynamicForm({ minAge }: { minAge: number }) {
// Schema only recreated when minAge changes
const userSchema = useMemo(
() =>
z.object({
name: z.string().min(1),
age: z.number().min(minAge)
}),
[minAge]
);
// ...
}
```
**For server-side, use module cache:**
```typescript
// schemas/user.ts - created once per process
import { z } from "zod";
export const userSchema = z.object({
id: z.string().uuid(),
email: z.string().email()
});
// api/users.ts
import { userSchema } from "@/schemas/user";
export async function POST(req: Request) {
const body = await req.json();
const result = userSchema.safeParse(body); // Reuses cached schema
// ...
}
```
**Avoid schema factories in hot paths:**
```typescript
// BAD: Factory called on every validation
function createUserSchema(role: string) {
return z.object({
name: z.string(),
permissions: z.array(z.string())
});
}
// Called in hot loop
users.forEach((user) => {
createUserSchema(user.role).parse(user); // New schema every iteration!
});
// GOOD: Cache by key
const schemaCache = new Map<string, z.ZodObject<any>>();
function getUserSchema(role: string) {
if (!schemaCache.has(role)) {
schemaCache.set(
role,
z.object({
name: z.string(),
permissions: z.array(z.string())
})
);
}
return schemaCache.get(role)!;
}
// Reuses cached schemas
users.forEach((user) => {
getUserSchema(user.role).parse(user);
});
```
**When NOT to use this pattern:**
- One-off validation where schema is used once
- Test files where performance doesn't matter
Reference: [Zod Performance](https://zod.dev/v4#performance)