Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 571ab0c

Browse files
authored
Drop JIT in Zod Mini z.object() (reduces bundle size by 20%) (#5051)
* WIP * WIP * No comments
1 parent d3ea111 commit 571ab0c

File tree

6 files changed

+175
-133
lines changed

6 files changed

+175
-133
lines changed

packages/bench/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ async function run() {
66
const files = process.argv[2].split(",").map((file) => import.meta.resolve(`./${file}`).replace("file://", ""));
77

88
for (const file of files) {
9-
// await $`pnpm tsx --conditions @zod/source ${file}`;
10-
await $`pnpm tsx ${file}`;
9+
await $`pnpm tsx --conditions @zod/source ${file}`;
10+
// await $`pnpm tsx ${file}`;
1111
}
1212
}
1313

packages/bench/object-moltar.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@ import * as z4lib from "zod4/v4";
33
import * as z3 from "zod3";
44
import { metabench } from "./metabench.js";
55

6-
const z3Schema = z3.object({
6+
const z3Schema = z3.strictObject({
77
number: z3.number(),
88
negNumber: z3.number(),
99
maxNumber: z3.number(),
1010
string: z3.string(),
1111
longString: z3.string(),
1212
boolean: z3.boolean(),
13-
deeplyNested: z3.object({
13+
deeplyNested: z3.strictObject({
1414
foo: z3.string(),
1515
num: z3.number(),
1616
bool: z3.boolean(),
1717
})
1818
})
1919

20-
const z4LibSchema = z4lib.object({
20+
const z4LibSchema = z4lib.strictObject({
2121
number: z4lib.number(),
2222
negNumber: z4lib.number(),
2323
maxNumber: z4lib.number(),
2424
string: z4lib.string(),
2525
longString: z4lib.string(),
2626
boolean: z4lib.boolean(),
27-
deeplyNested: z4lib.object({
27+
deeplyNested: z4lib.strictObject({
2828
foo: z4lib.string(),
2929
num: z4lib.number(),
3030
bool: z4lib.boolean(),
3131
}),
3232
});
3333

34-
const z4Schema = z4.object({
34+
const z4Schema = z4.strictObject({
3535
number: z4.number(),
3636
negNumber: z4.number(),
3737
maxNumber: z4.number(),
3838
string: z4.string(),
3939
longString: z4.string(),
4040
boolean: z4.boolean(),
41-
deeplyNested: z4.object({
41+
deeplyNested: z4.strictObject({
4242
foo: z4.string(),
4343
num: z4.number(),
4444
bool: z4.boolean(),

packages/treeshake/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"bundle": "rollup -c rollup.config.js --input",
88
"bundle:rollup": "rollup --input",
9-
"bundle:esbuild": "esbuild --bundle ./in.ts --outfile=./out.js --bundle --format=esm"
9+
"bundle:esbuild": "esbuild --bundle ./in.ts --conditions=@zod/source --outfile=./out.js --bundle --format=esm"
1010
},
1111
"devDependencies": {
1212
"@rollup/plugin-commonjs": "^28.0.3",

packages/treeshake/rollup.config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ export default {
1414
// sourcemap: true, // Generate sourcemaps for easier debugging
1515
},
1616
plugins: [
17-
resolve({
18-
19-
}), // Resolve node_modules
17+
resolve(), // Resolve node_modules
2018
commonjs(), // Convert CommonJS modules to ES6
2119
typescript(), // Compile TypeScript
22-
// bundleSize(),
2320
filesize(), // Display bundle size
24-
// terser(),
2521
],
2622
treeshake: {
2723
preset: "smallest",

packages/zod/src/v4/classic/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ export interface ZodObject<
11481148
}
11491149

11501150
export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
1151-
core.$ZodObject.init(inst, def);
1151+
core.$ZodObjectJIT.init(inst, def);
11521152
ZodType.init(inst, def);
11531153

11541154
util.defineLazy(inst, "shape", () => def.shape);

0 commit comments

Comments
 (0)