Skip to content

Commit ef3c444

Browse files
committed
perf(compiler): short circuit when no routes exist
1 parent 082f20f commit ef3c444

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

src/compiler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ function compileRouteMatch(
9797
str += "let s=p.split('/'),l=s.length-1;" + match;
9898
}
9999

100+
if (!str) {
101+
return opts?.matchAll ? "return [];" : "";
102+
}
103+
100104
return `${opts?.matchAll ? `let r=[];` : ""}if(p[p.length-1]==='/')p=p.slice(0,-1)||'/';${str}${opts?.matchAll ? "return r;" : ""}`;
101105
}
102106

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(m, p) => {
2+
return [];
3+
};

test/.snapshot/compiled-empty.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(m, p) => {};

test/find-all.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ describe("find-matchAll: basic", () => {
5151
).toMatchFileSnapshot(".snapshot/compiled-all.mjs");
5252
});
5353

54+
it("snapshot (compiled - empty)", async () => {
55+
await expect(
56+
await format(
57+
compileRouter(createRouter([]), { matchAll: true }).toString(),
58+
{
59+
parser: "acorn",
60+
},
61+
),
62+
).toMatchFileSnapshot(".snapshot/compiled-all-empty.mjs");
63+
});
64+
5465
it("matches /foo/bar/baz pattern", () => {
5566
const matches = _findAllRoutes(router, "GET", "/foo/bar/baz");
5667
expect(matches).to.toMatchInlineSnapshot(`

test/find.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ describe("route matching", () => {
5555
).toMatchFileSnapshot(".snapshot/compiled-aot.mjs");
5656
});
5757

58+
it("snapshot (compiled - empty)", async () => {
59+
await expect(
60+
await format(compileRouter(createRouter([])).toString(), {
61+
parser: "acorn",
62+
}),
63+
).toMatchFileSnapshot(".snapshot/compiled-empty.mjs");
64+
});
65+
5866
const lookups = [
5967
{
6068
name: "findRoute",

0 commit comments

Comments
 (0)