Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions express-zod-api/bench/can-merge.bench.ts

This file was deleted.

43 changes: 43 additions & 0 deletions express-zod-api/bench/queue-ops.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { bench, describe } from "vitest";

const smallQueue = Array.from({ length: 5 }, (_, i) => ({
id: i,
data: `item-${i}`,
}));
const mediumQueue = Array.from({ length: 20 }, (_, i) => ({
id: i,
data: `item-${i}`,
}));
const largeQueue = Array.from({ length: 100 }, (_, i) => ({
id: i,
data: `item-${i}`,
}));

const queues = [
[smallQueue, "small (5)"],
[mediumQueue, "medium (20)"],
[largeQueue, "large (100)"],
] as const;

describe.each(queues)("$1", (queue) => {
bench("shift() approach", () => {
const q = [...queue];
while (q.length) q.shift();
});

bench("index approach", () => {
const q = [...queue];
let idx = 0;
while (idx < q.length) idx++;
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

bench("pop() reverse", () => {
const q = [...queue].reverse();
while (q.length) q.pop();
});

bench("forEach", () => {
const q = [...queue];
q.forEach(() => {});
});
});
5 changes: 3 additions & 2 deletions express-zod-api/src/deep-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export const hasCycle = (
) => {
const json = z.toJSONSchema(subject, { io, unrepresentable: "any" });
const stack: unknown[] = [json];
while (stack.length) {
const entry = stack.shift()!;
let idx = 0;
while (idx < stack.length) {
const entry = stack[idx++];
if (R.is(Object, entry)) {
if ((entry as z.core.JSONSchema.BaseSchema).$ref === "#") return true;
stack.push(...R.values(entry));
Expand Down
5 changes: 3 additions & 2 deletions express-zod-api/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export class Diagnostics {
const stack: z.core.JSONSchema.BaseSchema[] = [
z.toJSONSchema(endpoint[`${dir}Schema`], { unrepresentable: "any" }),
];
while (stack.length > 0) {
const entry = stack.shift()!;
let idx = 0;
while (idx < stack.length) {
const entry = stack[idx++];
if (entry.type && entry.type !== "object")
this.logger.warn(`Endpoint ${dir} schema is not object-based`, ctx);
for (const prop of ["allOf", "oneOf", "anyOf"] as const)
Expand Down
5 changes: 3 additions & 2 deletions express-zod-api/src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,9 @@ const fixReferences = (
ctx: OpenAPIContext,
) => {
const stack: unknown[] = [subject, defs];
while (stack.length) {
const entry = stack.shift()!;
let idx = 0;
while (idx < stack.length) {
const entry = stack[idx++];
if (R.is(Object, entry)) {
if (isReferenceObject(entry) && !entry.$ref.startsWith("#/components")) {
const actualName = entry.$ref.split("/").pop()!;
Expand Down
5 changes: 3 additions & 2 deletions express-zod-api/src/json-schema-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ export const flattenIO = (
const stack: Stack = [R.pair(false, jsonSchema)]; // [isOptional, JSON Schema]
const flat: FlattenObjectSchema = { type: "object", properties: {} };
const flatRequired: string[] = [];
while (stack.length) {
const [isOptional, entry] = stack.shift()!;
let idx = 0;
while (idx < stack.length) {
const [isOptional, entry] = stack[idx++];
if (entry.description) flat.description ??= entry.description;
stack.push(...processAllOf(entry, mode, isOptional));
stack.push(...processVariants(entry));
Expand Down
7 changes: 4 additions & 3 deletions express-zod-api/src/routing-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ export const walkRouting = ({
}: RoutingWalkerParams) => {
const stack = processEntries(config, routing);
const visited = new Set<string>();
while (stack.length) {
const [path, element, explicitMethod] = stack.shift()!;
let idx = 0;
while (idx < stack.length) {
const [path, element, explicitMethod] = stack[idx++];
if (element instanceof AbstractEndpoint) {
if (explicitMethod) {
checkDuplicate(explicitMethod, path, visited);
Expand All @@ -110,7 +111,7 @@ export const walkRouting = ({
if (element instanceof ServeStatic) {
if (onStatic) element.apply(path, onStatic);
} else {
stack.unshift(...processEntries(config, element, path));
stack.splice(idx, 0, ...processEntries(config, element, path));
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions express-zod-api/src/zts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ const onTemplateLiteral: Producer = (
{ next, api },
) => {
const parts = [...def.parts];
let idx = 0;
const shiftText = () => {
let text = "";
while (parts.length) {
const part = parts.shift();
while (idx < parts.length) {
const part = parts[idx++];
if (isSchema(part)) {
parts.unshift(part);
idx--;
break;
}
text += part ?? ""; // Handle potential undefined values
Expand All @@ -55,12 +56,13 @@ const onTemplateLiteral: Producer = (
};
const head = api.f.createTemplateHead(shiftText());
const spans: ts.TemplateLiteralTypeSpan[] = [];
while (parts.length) {
const schema = next(parts.shift() as z.core.$ZodType);
while (idx < parts.length) {
const schema = next(parts[idx++] as z.core.$ZodType);
const text = shiftText();
const textWrapper = parts.length
? api.f.createTemplateMiddle
: api.f.createTemplateTail;
const textWrapper =
idx < parts.length
? api.f.createTemplateMiddle
: api.f.createTemplateTail;
spans.push(api.f.createTemplateLiteralTypeSpan(schema, textWrapper(text)));
}
if (!spans.length) return api.makeLiteralType(head.text);
Expand Down
Loading