From 76d2fe0b321e8211d470b7d340780218e6fab941 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Fri, 18 Mar 2022 08:28:45 -0400 Subject: [PATCH] Faster optional & nullable values --- coverage.svg | 2 +- deno/lib/benchmarks/string.ts | 11 +++++++++++ deno/lib/types.ts | 14 ++------------ src/benchmarks/string.ts | 11 +++++++++++ src/types.ts | 14 ++------------ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/coverage.svg b/coverage.svg index d1bfd484d..328443540 100644 --- a/coverage.svg +++ b/coverage.svg @@ -1 +1 @@ -Coverage: 96.16%Coverage96.16% +Coverage: 95.25%Coverage95.25% \ No newline at end of file diff --git a/deno/lib/benchmarks/string.ts b/deno/lib/benchmarks/string.ts index 39894f7a0..7278eaf8f 100644 --- a/deno/lib/benchmarks/string.ts +++ b/deno/lib/benchmarks/string.ts @@ -16,6 +16,8 @@ const manual = (str: unknown) => { return str; }; const stringSchema = z.string(); +const optionalStringSchema = z.string().optional(); +const optionalNullableStringSchema = z.string().optional().nullable(); suite .add("empty string", () => { @@ -27,6 +29,15 @@ suite .add("long string", () => { stringSchema.parse(long); }) + .add("optional string", () => { + optionalStringSchema.parse(long); + }) + .add("nullable string", () => { + optionalNullableStringSchema.parse(long); + }) + .add("nullable (null) string", () => { + optionalNullableStringSchema.parse(null); + }) .add("invalid: null", () => { try { stringSchema.parse(null); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 4aa3b0814..0945492e7 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -3421,12 +3421,7 @@ export class ZodOptional extends ZodType< if (parsedType === ZodParsedType.undefined) { return OK(undefined); } - const { ctx } = this._processInputParams(input); - return this._def.innerType._parse({ - data: ctx.data, - path: ctx.path, - parent: ctx, - }); + return this._def.innerType._parse(input); } unwrap() { @@ -3470,12 +3465,7 @@ export class ZodNullable extends ZodType< if (parsedType === ZodParsedType.null) { return OK(null); } - const { ctx } = this._processInputParams(input); - return this._def.innerType._parse({ - data: ctx.data, - path: ctx.path, - parent: ctx, - }); + return this._def.innerType._parse(input); } unwrap() { diff --git a/src/benchmarks/string.ts b/src/benchmarks/string.ts index 8d4ce54ae..cb3049e36 100644 --- a/src/benchmarks/string.ts +++ b/src/benchmarks/string.ts @@ -16,6 +16,8 @@ const manual = (str: unknown) => { return str; }; const stringSchema = z.string(); +const optionalStringSchema = z.string().optional(); +const optionalNullableStringSchema = z.string().optional().nullable(); suite .add("empty string", () => { @@ -27,6 +29,15 @@ suite .add("long string", () => { stringSchema.parse(long); }) + .add("optional string", () => { + optionalStringSchema.parse(long); + }) + .add("nullable string", () => { + optionalNullableStringSchema.parse(long); + }) + .add("nullable (null) string", () => { + optionalNullableStringSchema.parse(null); + }) .add("invalid: null", () => { try { stringSchema.parse(null); diff --git a/src/types.ts b/src/types.ts index 7ea81df47..6ede7975a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3421,12 +3421,7 @@ export class ZodOptional extends ZodType< if (parsedType === ZodParsedType.undefined) { return OK(undefined); } - const { ctx } = this._processInputParams(input); - return this._def.innerType._parse({ - data: ctx.data, - path: ctx.path, - parent: ctx, - }); + return this._def.innerType._parse(input); } unwrap() { @@ -3470,12 +3465,7 @@ export class ZodNullable extends ZodType< if (parsedType === ZodParsedType.null) { return OK(null); } - const { ctx } = this._processInputParams(input); - return this._def.innerType._parse({ - data: ctx.data, - path: ctx.path, - parent: ctx, - }); + return this._def.innerType._parse(input); } unwrap() {