Skip to content

Commit

Permalink
refactor: reduce bundle overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 24, 2024
1 parent 88a21d7 commit 0eed2e8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const EmptyObject = /* @__PURE__ */ (() => {
const C = function () {};
C.prototype = Object.create(null);
return C;
})() as unknown as { new (): any };
9 changes: 2 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { EmptyObject } from "./_utils";
import type { RouterContext } from "./types";

const RouterStaticMap = /* @__PURE__ */ (() => {
const C = function () {};
C.prototype = Object.create(null);
return C;
})() as unknown as { new (): Record<string, any> };

/**
* Create a new router context.
*/
export function createRouter<T = unknown>(): RouterContext<T> {
const ctx: RouterContext<T> = {
root: { key: "" },
static: new RouterStaticMap(),
static: new EmptyObject(),
};
return ctx;
}
9 changes: 2 additions & 7 deletions src/operations/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { EmptyObject } from "../_utils";
import type { MatchedRoute, ParamsIndexMap } from "../types";

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

const RouteParams = /* @__PURE__ */ (() => {
const C = function RouteParams() {};
C.prototype = Object.create(null);
return C;
})() as unknown as { new (): Record<string, any> };

export function getMatchParams(
segments: string[],
paramsMap: ParamsIndexMap,
): MatchedRoute["params"] {
const params = new RouteParams();
const params = new EmptyObject();
for (const [index, name] of paramsMap) {
const segment =
index < 0 ? segments.slice(-1 * index).join("/") : segments[index];
Expand Down
17 changes: 3 additions & 14 deletions src/operations/add.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { EmptyObject } from "../_utils";
import type { RouterContext, ParamsIndexMap } from "../types";
import { splitPath } from "./_utils";

const NodeStaticMap = /* @__PURE__ */ (() => {
const C = function () {};
C.prototype = Object.create(null);
return C;
})() as unknown as { new (): Record<string, any> };

const NodeMethodsMap = /* @__PURE__ */ (() => {
const C = function () {};
C.prototype = Object.create(null);
return C;
})() as unknown as { new (): Record<string, any> };

/**
* Add a route to the router context.
*/
Expand Down Expand Up @@ -71,7 +60,7 @@ export function addRoute<T>(
} else {
const staticNode = { key: segment };
if (!node.static) {
node.static = new NodeStaticMap();
node.static = new EmptyObject();
}
node.static![segment] = staticNode;
node = staticNode;
Expand All @@ -81,7 +70,7 @@ export function addRoute<T>(
// Assign index, params and data to the node
const hasParams = paramsMap.length > 0;
if (!node.methods) {
node.methods = new NodeMethodsMap();
node.methods = new EmptyObject();
}
if (!node.methods![method]) {
node.methods![method] = [];
Expand Down
4 changes: 2 additions & 2 deletions test/bench/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe("benchmark", () => {
`;
const { bytes, gzipSize } = await getBundleSize(code);
console.log("bundle size", { bytes, gzipSize });
expect(bytes).toBeLessThanOrEqual(3000); // <3kb
expect(gzipSize).toBeLessThanOrEqual(1500); // <1.5kb
expect(bytes).toBeLessThanOrEqual(2700); // <2.7kb
expect(gzipSize).toBeLessThanOrEqual(1100); // <1.1kb
});
});

Expand Down

0 comments on commit 0eed2e8

Please sign in to comment.