realworld.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Benchmark from "benchmark";
  2. import { z } from "zod/v3";
  3. const shortSuite = new Benchmark.Suite("realworld");
  4. const People = z.array(z.object({
  5. type: z.literal("person"),
  6. hair: z.enum(["blue", "brown"]),
  7. active: z.boolean(),
  8. name: z.string(),
  9. age: z.number().int(),
  10. hobbies: z.array(z.string()),
  11. address: z.object({
  12. street: z.string(),
  13. zip: z.string(),
  14. country: z.string(),
  15. }),
  16. }));
  17. let i = 0;
  18. function num() {
  19. return ++i;
  20. }
  21. function str() {
  22. return (++i % 100).toString(16);
  23. }
  24. function array(fn) {
  25. return Array.from({ length: ++i % 10 }, () => fn());
  26. }
  27. const people = Array.from({ length: 100 }, () => {
  28. return {
  29. type: "person",
  30. hair: i % 2 ? "blue" : "brown",
  31. active: !!(i % 2),
  32. name: str(),
  33. age: num(),
  34. hobbies: array(str),
  35. address: {
  36. street: str(),
  37. zip: str(),
  38. country: str(),
  39. },
  40. };
  41. });
  42. shortSuite
  43. .add("valid", () => {
  44. People.parse(people);
  45. })
  46. .on("cycle", (e) => {
  47. console.log(`${shortSuite.name}: ${e.target}`);
  48. });
  49. export default {
  50. suites: [shortSuite],
  51. };