realworld.js 1.5 KB

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