From 221037f447741596cd71525f788cb1311172000f Mon Sep 17 00:00:00 2001 From: Bonomi Jacopo Date: Mon, 20 Oct 2025 15:03:15 +0200 Subject: [PATCH 1/2] perf: lazy initialize ~standard schema property --- packages/zod/src/v4/core/schemas.ts | 39 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/zod/src/v4/core/schemas.ts b/packages/zod/src/v4/core/schemas.ts index 78cb4749f..8fd2787a5 100644 --- a/packages/zod/src/v4/core/schemas.ts +++ b/packages/zod/src/v4/core/schemas.ts @@ -301,18 +301,35 @@ export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constru }; } - inst["~standard"] = { - validate: (value: unknown) => { - try { - const r = safeParse(inst, value); - return r.success ? { value: r.data } : { issues: r.error?.issues }; - } catch (_) { - return safeParseAsync(inst, value).then((r) => (r.success ? { value: r.data } : { issues: r.error?.issues })); - } + // Lazy initialize ~standard to avoid creating objects for every schema + Object.defineProperty(inst, "~standard", { + get() { + const standardSchema = { + validate: (value: unknown) => { + try { + const r = safeParse(inst, value); + return r.success ? { value: r.data } : { issues: r.error?.issues }; + } catch (_) { + return safeParseAsync(inst, value).then((r) => + r.success ? { value: r.data } : { issues: r.error?.issues } + ); + } + }, + vendor: "zod", + version: 1 as const, + }; + // Cache the result after first access + Object.defineProperty(this, "~standard", { + value: standardSchema, + writable: false, + enumerable: false, + configurable: false, + }); + return standardSchema; }, - vendor: "zod", - version: 1 as const, - }; + enumerable: false, + configurable: true, + }); }); export { clone } from "./util.js"; From 6a7cdb180f45c6761c059c7049cc1c41210fcfc2 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Thu, 6 Nov 2025 12:54:43 -0800 Subject: [PATCH 2/2] Use definelazy --- packages/zod/src/v4/core/schemas.ts | 38 +++++++++-------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/packages/zod/src/v4/core/schemas.ts b/packages/zod/src/v4/core/schemas.ts index 8fd2787a5..eb12129d1 100644 --- a/packages/zod/src/v4/core/schemas.ts +++ b/packages/zod/src/v4/core/schemas.ts @@ -302,34 +302,18 @@ export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constru } // Lazy initialize ~standard to avoid creating objects for every schema - Object.defineProperty(inst, "~standard", { - get() { - const standardSchema = { - validate: (value: unknown) => { - try { - const r = safeParse(inst, value); - return r.success ? { value: r.data } : { issues: r.error?.issues }; - } catch (_) { - return safeParseAsync(inst, value).then((r) => - r.success ? { value: r.data } : { issues: r.error?.issues } - ); - } - }, - vendor: "zod", - version: 1 as const, - }; - // Cache the result after first access - Object.defineProperty(this, "~standard", { - value: standardSchema, - writable: false, - enumerable: false, - configurable: false, - }); - return standardSchema; + util.defineLazy(inst, "~standard", () => ({ + validate: (value: unknown) => { + try { + const r = safeParse(inst, value); + return r.success ? { value: r.data } : { issues: r.error?.issues }; + } catch (_) { + return safeParseAsync(inst, value).then((r) => (r.success ? { value: r.data } : { issues: r.error?.issues })); + } }, - enumerable: false, - configurable: true, - }); + vendor: "zod", + version: 1 as const, + })); }); export { clone } from "./util.js";