object.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Benchmark from "benchmark";
  2. import { z } from "zod/v3";
  3. const emptySuite = new Benchmark.Suite("z.object: empty");
  4. const shortSuite = new Benchmark.Suite("z.object: short");
  5. const longSuite = new Benchmark.Suite("z.object: long");
  6. const empty = z.object({});
  7. const short = z.object({
  8. string: z.string(),
  9. });
  10. const long = z.object({
  11. string: z.string(),
  12. number: z.number(),
  13. boolean: z.boolean(),
  14. });
  15. emptySuite
  16. .add("valid", () => {
  17. empty.parse({});
  18. })
  19. .add("valid: extra keys", () => {
  20. empty.parse({ string: "string" });
  21. })
  22. .add("invalid: null", () => {
  23. try {
  24. empty.parse(null);
  25. }
  26. catch (_err) { }
  27. })
  28. .on("cycle", (e) => {
  29. console.log(`${emptySuite.name}: ${e.target}`);
  30. });
  31. shortSuite
  32. .add("valid", () => {
  33. short.parse({ string: "string" });
  34. })
  35. .add("valid: extra keys", () => {
  36. short.parse({ string: "string", number: 42 });
  37. })
  38. .add("invalid: null", () => {
  39. try {
  40. short.parse(null);
  41. }
  42. catch (_err) { }
  43. })
  44. .on("cycle", (e) => {
  45. console.log(`${shortSuite.name}: ${e.target}`);
  46. });
  47. longSuite
  48. .add("valid", () => {
  49. long.parse({ string: "string", number: 42, boolean: true });
  50. })
  51. .add("valid: extra keys", () => {
  52. long.parse({ string: "string", number: 42, boolean: true, list: [] });
  53. })
  54. .add("invalid: null", () => {
  55. try {
  56. long.parse(null);
  57. }
  58. catch (_err) { }
  59. })
  60. .on("cycle", (e) => {
  61. console.log(`${longSuite.name}: ${e.target}`);
  62. });
  63. export default {
  64. suites: [emptySuite, shortSuite, longSuite],
  65. };