Skip to content

Commit 0eed2e8

Browse files
committed
refactor: reduce bundle overhead
1 parent 88a21d7 commit 0eed2e8

File tree

5 files changed

+14
-30
lines changed

5 files changed

+14
-30
lines changed

src/_utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const EmptyObject = /* @__PURE__ */ (() => {
2+
const C = function () {};
3+
C.prototype = Object.create(null);
4+
return C;
5+
})() as unknown as { new (): any };

src/context.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1+
import { EmptyObject } from "./_utils";
12
import type { RouterContext } from "./types";
23

3-
const RouterStaticMap = /* @__PURE__ */ (() => {
4-
const C = function () {};
5-
C.prototype = Object.create(null);
6-
return C;
7-
})() as unknown as { new (): Record<string, any> };
8-
94
/**
105
* Create a new router context.
116
*/
127
export function createRouter<T = unknown>(): RouterContext<T> {
138
const ctx: RouterContext<T> = {
149
root: { key: "" },
15-
static: new RouterStaticMap(),
10+
static: new EmptyObject(),
1611
};
1712
return ctx;
1813
}

src/operations/_utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1+
import { EmptyObject } from "../_utils";
12
import type { MatchedRoute, ParamsIndexMap } from "../types";
23

34
export function splitPath(path: string) {
45
return path.split("/").filter(Boolean);
56
}
67

7-
const RouteParams = /* @__PURE__ */ (() => {
8-
const C = function RouteParams() {};
9-
C.prototype = Object.create(null);
10-
return C;
11-
})() as unknown as { new (): Record<string, any> };
12-
138
export function getMatchParams(
149
segments: string[],
1510
paramsMap: ParamsIndexMap,
1611
): MatchedRoute["params"] {
17-
const params = new RouteParams();
12+
const params = new EmptyObject();
1813
for (const [index, name] of paramsMap) {
1914
const segment =
2015
index < 0 ? segments.slice(-1 * index).join("/") : segments[index];

src/operations/add.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1+
import { EmptyObject } from "../_utils";
12
import type { RouterContext, ParamsIndexMap } from "../types";
23
import { splitPath } from "./_utils";
34

4-
const NodeStaticMap = /* @__PURE__ */ (() => {
5-
const C = function () {};
6-
C.prototype = Object.create(null);
7-
return C;
8-
})() as unknown as { new (): Record<string, any> };
9-
10-
const NodeMethodsMap = /* @__PURE__ */ (() => {
11-
const C = function () {};
12-
C.prototype = Object.create(null);
13-
return C;
14-
})() as unknown as { new (): Record<string, any> };
15-
165
/**
176
* Add a route to the router context.
187
*/
@@ -71,7 +60,7 @@ export function addRoute<T>(
7160
} else {
7261
const staticNode = { key: segment };
7362
if (!node.static) {
74-
node.static = new NodeStaticMap();
63+
node.static = new EmptyObject();
7564
}
7665
node.static![segment] = staticNode;
7766
node = staticNode;
@@ -81,7 +70,7 @@ export function addRoute<T>(
8170
// Assign index, params and data to the node
8271
const hasParams = paramsMap.length > 0;
8372
if (!node.methods) {
84-
node.methods = new NodeMethodsMap();
73+
node.methods = new EmptyObject();
8574
}
8675
if (!node.methods![method]) {
8776
node.methods![method] = [];

test/bench/bundle.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe("benchmark", () => {
1414
`;
1515
const { bytes, gzipSize } = await getBundleSize(code);
1616
console.log("bundle size", { bytes, gzipSize });
17-
expect(bytes).toBeLessThanOrEqual(3000); // <3kb
18-
expect(gzipSize).toBeLessThanOrEqual(1500); // <1.5kb
17+
expect(bytes).toBeLessThanOrEqual(2700); // <2.7kb
18+
expect(gzipSize).toBeLessThanOrEqual(1100); // <1.1kb
1919
});
2020
});
2121

0 commit comments

Comments
 (0)