Skip to content

Commit

Permalink
Fully remove typeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 30, 2022
1 parent d109f67 commit 222df66
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
103 changes: 103 additions & 0 deletions deno/lib/benchmarks/competitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import Benchmark from "benchmark";
import mz from "myzod";

import { z } from "../index.ts";

const shortSuite = new Benchmark.Suite("competitive");

const MPeople = mz.array(
mz.object({
type: mz.literal("person"),
hair: mz.enum(["blue", "brown"]),
active: mz.boolean(),
name: mz.string(),
age: mz.number(),
hobbies: mz.array(mz.string()),
address: mz.object({
street: mz.string(),
zip: mz.string(),
country: mz.string(),
}),
})
);

const People = z.array(
z.object({
type: z.literal("person"),
hair: z.enum(["blue", "brown"]),
active: z.boolean(),
name: z.string(),
age: z.number(),
hobbies: z.array(z.string()),
address: z.object({
street: z.string(),
zip: z.string(),
country: z.string(),
}),
})
);

let i = 0;

function num() {
return ++i;
}

function str() {
return (++i % 100).toString(16);
}

function array<T>(fn: () => T): T[] {
return Array.from({ length: ++i % 10 }, () => fn());
}

const people = Array.from({ length: 100 }, () => {
return {
type: "person",
hair: i % 2 ? "blue" : "brown",
active: !!(i % 2),
name: str(),
age: num(),
hobbies: array(str),
address: {
street: str(),
zip: str(),
country: str(),
},
};
});

const zString = z.string();
const mzString = mz.string();
const zEnum = z.enum(["blue", "brown"]);
const mzEnum = mz.enum(["blue", "brown"]);

shortSuite

.add("zod: string", () => {
zString.parse("foo");
})
.add("mz: string", () => {
mzString.parse("foo");
})
.add("zod: enum", () => {
zEnum.parse("blue");
})
.add("mz: enum", () => {
mzEnum.parse("blue");
})
.add("zod", () => {
People.parse(people);
})
.add("myzod", () => {
MPeople.parse(people);
})
.on("cycle", (e: Benchmark.Event) => {
console.log(`${(shortSuite as any).name}: ${e.target}`);
});

export default {
suites: [shortSuite],
};

shortSuite.run();
2 changes: 0 additions & 2 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export abstract class ZodType<
common: {
issues: [],
async: params?.async ?? false,
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
contextualErrorMap: params?.errorMap,
},
path: params?.path || [],
Expand Down Expand Up @@ -268,7 +267,6 @@ export abstract class ZodType<
issues: [],
contextualErrorMap: params?.errorMap,
async: true,
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
},
path: params?.path || [],
schemaErrorMap: this._def.errorMap,
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export abstract class ZodType<
common: {
issues: [],
async: params?.async ?? false,
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
contextualErrorMap: params?.errorMap,
},
path: params?.path || [],
Expand Down Expand Up @@ -268,7 +267,6 @@ export abstract class ZodType<
issues: [],
contextualErrorMap: params?.errorMap,
async: true,
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
},
path: params?.path || [],
schemaErrorMap: this._def.errorMap,
Expand Down

0 comments on commit 222df66

Please sign in to comment.