Skip to content

Commit

Permalink
Faster optional & nullable values
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 18, 2022
1 parent 277d434 commit 76d2fe0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion coverage.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions deno/lib/benchmarks/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3421,12 +3421,7 @@ export class ZodOptional<T extends ZodTypeAny> 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() {
Expand Down Expand Up @@ -3470,12 +3465,7 @@ export class ZodNullable<T extends ZodTypeAny> 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() {
Expand Down
11 changes: 11 additions & 0 deletions src/benchmarks/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3421,12 +3421,7 @@ export class ZodOptional<T extends ZodTypeAny> 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() {
Expand Down Expand Up @@ -3470,12 +3465,7 @@ export class ZodNullable<T extends ZodTypeAny> 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() {
Expand Down

0 comments on commit 76d2fe0

Please sign in to comment.