/zod-4:optimizing-performancefrom zod-4
Optimize Zod validation performance with safeParse, bulk array validation, schema reuse, and passthrough patterns
Ships in zod-4. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Guide to performance optimization patterns in Zod v4, leveraging improved parsing speed, reduced bundle size, and efficient validation strategies.
Zod v4 introduced major performance gains:
These improvements are automatic when using v4.
SafeParse is 20-30% faster for invalid data:
const result = schema.safeParse(data);
if (!result.success) {
console.error(result.error);
return;
}
return result.data;
Why faster:
When to use parse: Internal data that should always be valid (errors are bugs).
Define schemas at module level, not inside functions:
const userSchema = z.object({
email: z.email(),
name: z.string()
});
function validateUser(data: unknown) {
return userSchema.parse(data);
}
Performance improvement: 2-5x faster for repeated validations
Use z.array() instead of looping:
const arraySchema = z.array(itemSchema);
const result = arraySchema.safeParse(items);
7x faster in Zod v4 than item-by-item validation.
Use discriminator field for O(1) lookup:
const eventSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('click'), x: z.number(), y: z.number() }),
z.object({ type: z.literal('keypress'), key: z.string() })
]);
Much faster than regular unions which try each schema sequentially.
Use top-level functions for 14x faster parsing:
z.email() // ✅ v4 optimized
z.string().email() // ❌ v3 deprecated
All v4 string formats are optimized: z.email(), z.uuid(), z.url(), z.iso.datetime(), etc.
Skip property stripping when not needed:
const schema = z.object({
id: z.string(),
name: z.string()
}).passthrough();
10-20% faster than strict mode which strips unknown keys.
Use strict for: Security-sensitive data, API responses Use passthrough for: Performance-critical paths, partial validation
Place fast refinements first:
const schema = z.string()
.refine(val => val.length > 5) // Fast sync
.refine(val => /[A-Z]/.test(val)) // Slower sync
.refine(async val => checkDB(val)); // Async last
Use extend instead of merge (more performant):
const combined = schemaA.extend(schemaB.shape); // ✅
const combined = schemaA.merge(schemaB); // ❌
const loginSchema = userSchema.pick({
email: true,
password: true
});
Faster than redefining the schema.
Zod v4 is fully tree-shakeable. Just import and use:
import { z } from 'zod';
For extreme size constraints:
npm install zod-mini
Trade-offs: Smaller bundle (~2KB vs ~14KB), fewer features, same API for basics.
schema.safeParse(data) // ✅ 20-30% faster
z.array(schema).parse(items) // ✅ 7x faster bulk validation
z.discriminatedUnion('type', [...]) // ✅ O(1) lookup
z.email() // ✅ 14x faster v4 format
z.object({...}).passthrough() // ✅ 10-20% faster
const schema = z.object({...}) // ✅ Module level, reusable
.refine(fast).refine(slow).refine(async) // ✅ Fast first
schemaA.extend(schemaB.shape) // ✅ Faster than merge
Cross-Plugin References:
What SKILL.md sets; muted values are Claude Code's defaults
9 skills in this plugin
/customizing-errorsHandle Zod validation errors with unified error API, custom messages, error formatting, and user-friendly display/integrating-zod-frameworksIntegrate Zod v4 with React Hook Form, Next.js, Express, tRPC, and other frameworks for type-safe validation/migrating-v3-to-v4Complete migration guide from Zod v3 to v4 covering all breaking changes and upgrade patternsOther plugins' skills for the same job
/reviewing-patternsReview Zod schemas for correctness, performance, and v4 best practicesnpx claudepluginhub djankies/claude-configs --plugin zod-4