Skip to content

Commit

Permalink
[break] first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mathcovax committed Mar 8, 2024
1 parent c35c6b0 commit 3bac31d
Show file tree
Hide file tree
Showing 70 changed files with 4,077 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/TU.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: TU

on: [pull_request]

jobs:
tu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: npm ci
- run: npm run test:tu:coverage
- if: always()
uses: davelosert/vitest-coverage-report-action@v2
117 changes: 117 additions & 0 deletions benchmark/array.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import * as zod from "zod";
import joi from "joi";
import myzod from "myzod";
import {Type as typebox} from "@sinclair/typebox";
import {Value as typeboxValue} from "@sinclair/typebox/value";
import {ZodAccelerator} from "../scripts";
import Bench from "tinybench";

const zodSchema = zod.object({
firstname: zod.string(),
lastname: zod.string(),
age: zod.number(),
email: zod.string(),
gender: zod.enum(["boy", "girl"]),
connected: zod.boolean(),
createdAt: zod.date(),
addresse: zod.object({
postCode: zod.string(),
city: zod.string(),
number: zod.number()
}),
}).array();
const joiSchema = joi.array().items(
joi.object({
firstname: joi.string(),
lastname: joi.string(),
age: joi.number(),
email: joi.string(),
gender: joi.string().valid("boy", "girl"),
connected: joi.boolean(),
createdAt: joi.date(),
addresse: joi.object({
postCode: joi.string(),
city: joi.string(),
number: joi.number()
}),
})
);
const myzodSchema = myzod.array(
myzod.object({
firstname: myzod.string(),
lastname: myzod.string(),
age: myzod.number(),
email: myzod.string(),
gender: myzod.enum(["boy", "girl"]),
connected: myzod.boolean(),
createdAt: myzod.date(),
addresse: myzod.object({
postCode: myzod.string(),
city: myzod.string(),
number: myzod.number()
}),
})
);
enum typeboxGender {
boy,
girl
}
const typeboxSchema = typebox.Array(
typebox.Object({
firstname: typebox.String(),
lastname: typebox.String(),
age: typebox.Number(),
email: typebox.String(),
gender: typebox.Enum(typeboxGender),
connected: typebox.Boolean(),
createdAt: typebox.Date(),
addresse: typebox.Object({
postCode: typebox.String(),
city: typebox.String(),
number: typebox.Number()
}),
})
);
const zodAccelerateSchema = ZodAccelerator.build(zodSchema);

const bench = new Bench({time: 100});

const data = Array.from({length: 10}).fill({
firstname: " Mike ",
lastname: "ee",
age: 21,
email: "test@gmail.com",
gender: "girl",
connected: true,
createdAt: new Date(),
addresse: {
postCode: "22778",
city: "Paris",
number: 67
},
});

bench
.add("zod", () => {
zodSchema.parse(data);
})
.add("joi", () => {
joiSchema.validate(data);
})
.add("@sinclair/typebox", () => {
typeboxValue.Check(typeboxSchema, data);
})
.add("myzod", () => {
myzodSchema.parse(data);
})
.add("zodAccelerator", () => {
zodAccelerateSchema.parse(data);
});

(async() => {
await bench.warmup();
await bench.run();

console.log("Result test Array :");
console.table(bench.table());
})();
111 changes: 111 additions & 0 deletions benchmark/object.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as zod from "zod";
import joi from "joi";
import myzod from "myzod";
import {Type as typebox} from "@sinclair/typebox";
import {Value as typeboxValue} from "@sinclair/typebox/value";
import {ZodAccelerator} from "../scripts";
import Bench from "tinybench";

const zodSchema = zod.object({
firstname: zod.string(),
lastname: zod.string(),
age: zod.number(),
email: zod.string(),
gender: zod.enum(["boy", "girl"]),
connected: zod.boolean(),
createdAt: zod.date(),
addresse: zod.object({
postCode: zod.string(),
city: zod.string(),
number: zod.number()
}),
});
const joiSchema = joi.object({
firstname: joi.string(),
lastname: joi.string(),
age: joi.number(),
email: joi.string(),
gender: joi.string().valid("boy", "girl"),
connected: joi.boolean(),
createdAt: joi.date(),
addresse: joi.object({
postCode: joi.string(),
city: joi.string(),
number: joi.number()
}),
});
const myzodSchema = myzod.object({
firstname: myzod.string(),
lastname: myzod.string(),
age: myzod.number(),
email: myzod.string(),
gender: myzod.enum(["boy", "girl"]),
connected: myzod.boolean(),
createdAt: myzod.date(),
addresse: myzod.object({
postCode: myzod.string(),
city: myzod.string(),
number: myzod.number()
}),
});
enum typeboxGender {
boy,
girl
}
const typeboxSchema = typebox.Object({
firstname: typebox.String(),
lastname: typebox.String(),
age: typebox.Number(),
email: typebox.String(),
gender: typebox.Enum(typeboxGender),
connected: typebox.Boolean(),
createdAt: typebox.Date(),
addresse: typebox.Object({
postCode: typebox.String(),
city: typebox.String(),
number: typebox.Number()
}),
});
const zodAccelerateSchema = ZodAccelerator.build(zodSchema);

const bench = new Bench({time: 100});

const data = {
firstname: " Mike ",
lastname: "ee",
age: 21,
email: "test@gmail.com",
gender: "girl",
connected: true,
createdAt: new Date(),
addresse: {
postCode: "22778",
city: "Paris",
number: 67
},
};

bench
.add("zod", () => {
zodSchema.parse(data);
})
.add("joi", () => {
joiSchema.validate(data);
})
.add("@sinclair/typebox", () => {
typeboxValue.Check(typeboxSchema, data);
})
.add("myzod", () => {
myzodSchema.parse(data);
})
.add("zodAccelerator", () => {
zodAccelerateSchema.parse(data);
});

(async() => {
await bench.warmup();
await bench.run();

console.log("Result test Object :");
console.table(bench.table());
})();
41 changes: 41 additions & 0 deletions benchmark/string.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as zod from "zod";
import joi from "joi";
import myzod from "myzod";
import {Type as typebox} from "@sinclair/typebox";
import {Value as typeboxValue} from "@sinclair/typebox/value";
import {ZodAccelerator} from "../scripts";
import Bench from "tinybench";

const zodSchema = zod.string();
const joiSchema = joi.string();
const myzodSchema = myzod.string();
const typeboxSchema = typebox.String();
const zodAccelerateSchema = ZodAccelerator.build(zodSchema);

const bench = new Bench({time: 100});
const data = "this is a nice test";

bench
.add("zod", () => {
zodSchema.parse(data);
})
.add("joi", () => {
joiSchema.validate(data);
})
.add("@sinclair/typebox", () => {
typeboxValue.Check(typeboxSchema, data);
})
.add("myzod", () => {
myzodSchema.parse(data);
})
.add("zodAccelerator", () => {
zodAccelerateSchema.parse(data);
});

(async() => {
await bench.warmup();
await bench.run();

console.log("Result test String :");
console.table(bench.table());
})();
62 changes: 62 additions & 0 deletions benchmark/union.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as zod from "zod";
import joi from "joi";
import myzod from "myzod";
import {Type as typebox} from "@sinclair/typebox";
import {Value as typeboxValue} from "@sinclair/typebox/value";
import {ZodAccelerator} from "../scripts";
import Bench from "tinybench";

const zodSchema = zod.union([
zod.literal("123"),
zod.literal("456"),
zod.object({
test: zod.string()
}),
]);
const joiSchema = null;
const myzodSchema = myzod.union([
myzod.literal("123"),
myzod.literal("456"),
myzod.object({
test: myzod.string()
}),
]);
const typeboxSchema = typebox.Union([
typebox.Literal("123"),
typebox.Literal("456"),
typebox.Object({
test: typebox.String()
}),
]);
const zodAccelerateSchema = ZodAccelerator.build(zodSchema);


const bench = new Bench({time: 100});
const data = {
test: "test"
};

bench
.add("zod", () => {
zodSchema.parse(data);
})
.add("joi", () => {
throw joiSchema;
})
.add("@sinclair/typebox", () => {
typeboxValue.Check(typeboxSchema, data);
})
.add("myzod", () => {
myzodSchema.parse(data);
})
.add("zodAccelerator", () => {
zodAccelerateSchema.parse(data);
});

(async() => {
await bench.warmup();
await bench.run();

console.log("Result test Union :");
console.table(bench.table());
})();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"build": "rollup --config && npx tsc -p tsconfig.build.json",
"start": "node -r sucrase/register test.ts",
"test:bench": "node -r sucrase/register benchmark/string.bench.ts && node -r sucrase/register benchmark/object.bench.ts && node -r sucrase/register benchmark/array.bench.ts && node -r sucrase/register benchmark/union.bench.ts",
"test:coverage": "npx vitest --coverage",
"test:e2e": "node --require sucrase/register test",
"test:tu:coverage": "npx vitest --coverage",
"test:e2e": "node --require sucrase/register test/E2E",
"test:types": "tsc"
},
"dependencies": {
Expand Down
32 changes: 32 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://github.dev/colinhacks/zod

import "./lib/accelerators/string";
import "./lib/accelerators/number";
import "./lib/accelerators/object";
import "./lib/accelerators/array";
import "./lib/accelerators/enum";
import "./lib/accelerators/boolean";
import "./lib/accelerators/nullable";
import "./lib/accelerators/date";
import "./lib/accelerators/symbol";
import "./lib/accelerators/undefined";
import "./lib/accelerators/null";
import "./lib/accelerators/any";
import "./lib/accelerators/unknown";
import "./lib/accelerators/never";
import "./lib/accelerators/void";
import "./lib/accelerators/union";
import "./lib/accelerators/intersection";
import "./lib/accelerators/tuple";
import "./lib/accelerators/default";
import "./lib/accelerators/branded";
import "./lib/accelerators/nan";
import "./lib/accelerators/optional";
import "./lib/accelerators/catch";
import "./lib/accelerators/record";
import "./lib/accelerators/literal";
import "./lib/accelerators/readonly";
import "./lib/accelerators/bigInt";
import "./lib/accelerators/effects";

export * from "./lib/accelerator";
Loading

0 comments on commit 3bac31d

Please sign in to comment.