Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Set.has instead of Array.indexOf for enum comparison (perf improvement) #2659

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions deno/lib/benchmarks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ if (!argv.length) {
for (const suite of suites) {
suite.run();
}

// exit on Ctrl-C
process.on("SIGINT", function () {
console.log("Exiting...");
process.exit();
});
35 changes: 35 additions & 0 deletions deno/lib/benchmarks/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ enumSuite
console.log(`z.enum: ${e.target}`);
});

const longEnumSuite = new Benchmark.Suite("long z.enum");
const longEnumSchema = z.enum([
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
]);

longEnumSuite
.add("valid", () => {
longEnumSchema.parse("five");
})
.add("invalid", () => {
try {
longEnumSchema.parse("invalid");
} catch (e) {}
})
.on("cycle", (e: Benchmark.Event) => {
console.log(`long z.enum: ${e.target}`);
});

const undefinedSuite = new Benchmark.Suite("z.undefined");
const undefinedSchema = z.undefined();

Expand Down Expand Up @@ -129,6 +163,7 @@ symbolSuite
export default {
suites: [
enumSuite,
longEnumSuite,
undefinedSuite,
literalSuite,
numberSuite,
Expand Down
15 changes: 13 additions & 2 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4120,6 +4120,8 @@ export class ZodEnum<T extends [string, ...string[]]> extends ZodType<
T[number],
ZodEnumDef<T>
> {
#cache: Set<T[number]> | undefined;

_parse(input: ParseInput): ParseReturnType<this["_output"]> {
if (typeof input.data !== "string") {
const ctx = this._getOrReturnCtx(input);
Expand All @@ -4132,7 +4134,11 @@ export class ZodEnum<T extends [string, ...string[]]> extends ZodType<
return INVALID;
}

if (this._def.values.indexOf(input.data) === -1) {
if (!this.#cache) {
this.#cache = new Set(this._def.values);
}

if (!this.#cache.has(input.data)) {
const ctx = this._getOrReturnCtx(input);
const expectedValues = this._def.values;

Expand Down Expand Up @@ -4224,6 +4230,7 @@ export class ZodNativeEnum<T extends EnumLike> extends ZodType<
T[keyof T],
ZodNativeEnumDef<T>
> {
#cache: Set<T[keyof T]> | undefined;
_parse(input: ParseInput): ParseReturnType<T[keyof T]> {
const nativeEnumValues = util.getValidEnumValues(this._def.values);

Expand All @@ -4241,7 +4248,11 @@ export class ZodNativeEnum<T extends EnumLike> extends ZodType<
return INVALID;
}

if (nativeEnumValues.indexOf(input.data) === -1) {
if (!this.#cache) {
this.#cache = new Set(util.getValidEnumValues(this._def.values));
}

if (!this.#cache.has(input.data)) {
const expectedValues = util.objectValues(nativeEnumValues);

addIssueToContext(ctx, {
Expand Down
6 changes: 6 additions & 0 deletions src/benchmarks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ if (!argv.length) {
for (const suite of suites) {
suite.run();
}

// exit on Ctrl-C
process.on("SIGINT", function () {
console.log("Exiting...");
process.exit();
});
35 changes: 35 additions & 0 deletions src/benchmarks/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ enumSuite
console.log(`z.enum: ${e.target}`);
});

const longEnumSuite = new Benchmark.Suite("long z.enum");
const longEnumSchema = z.enum([
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
]);

longEnumSuite
.add("valid", () => {
longEnumSchema.parse("five");
})
.add("invalid", () => {
try {
longEnumSchema.parse("invalid");
} catch (e) {}
})
.on("cycle", (e: Benchmark.Event) => {
console.log(`long z.enum: ${e.target}`);
});

const undefinedSuite = new Benchmark.Suite("z.undefined");
const undefinedSchema = z.undefined();

Expand Down Expand Up @@ -129,6 +163,7 @@ symbolSuite
export default {
suites: [
enumSuite,
longEnumSuite,
undefinedSuite,
literalSuite,
numberSuite,
Expand Down
15 changes: 13 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4120,6 +4120,8 @@ export class ZodEnum<T extends [string, ...string[]]> extends ZodType<
T[number],
ZodEnumDef<T>
> {
#cache: Set<T[number]> | undefined;

_parse(input: ParseInput): ParseReturnType<this["_output"]> {
if (typeof input.data !== "string") {
const ctx = this._getOrReturnCtx(input);
Expand All @@ -4132,7 +4134,11 @@ export class ZodEnum<T extends [string, ...string[]]> extends ZodType<
return INVALID;
}

if (this._def.values.indexOf(input.data) === -1) {
if (!this.#cache) {
this.#cache = new Set(this._def.values);
}

if (!this.#cache.has(input.data)) {
const ctx = this._getOrReturnCtx(input);
const expectedValues = this._def.values;

Expand Down Expand Up @@ -4224,6 +4230,7 @@ export class ZodNativeEnum<T extends EnumLike> extends ZodType<
T[keyof T],
ZodNativeEnumDef<T>
> {
#cache: Set<T[keyof T]> | undefined;
_parse(input: ParseInput): ParseReturnType<T[keyof T]> {
const nativeEnumValues = util.getValidEnumValues(this._def.values);

Expand All @@ -4241,7 +4248,11 @@ export class ZodNativeEnum<T extends EnumLike> extends ZodType<
return INVALID;
}

if (nativeEnumValues.indexOf(input.data) === -1) {
if (!this.#cache) {
this.#cache = new Set(util.getValidEnumValues(this._def.values));
}

if (!this.#cache.has(input.data)) {
const expectedValues = util.objectValues(nativeEnumValues);

addIssueToContext(ctx, {
Expand Down
Loading