Skip to content

Commit

Permalink
perf: avoid Object.create(null)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 23, 2024
1 parent c8cd26a commit 2b7ac09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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: Object.create(null),
static: new RouterStaticMap(),
};
return ctx;
}
8 changes: 7 additions & 1 deletion src/operations/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ 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 = Object.create(null);
const params = new RouteParams();
for (const [index, name] of paramsMap) {
const segment =
index < 0 ? segments.slice(-1 * index).join("/") : segments[index];
Expand Down
16 changes: 14 additions & 2 deletions src/operations/add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 @@ -53,7 +65,7 @@ export function addRoute<T>(
} else {
const staticNode = { key: segment };
if (!node.static) {
node.static = Object.create(null);
node.static = new NodeStaticMap();
}
node.static![segment] = staticNode;
node = staticNode;
Expand All @@ -63,7 +75,7 @@ export function addRoute<T>(
// Assign index, params and data to the node
const hasParams = paramsMap.length > 0;
if (!node.methods) {
node.methods = Object.create(null);
node.methods = new NodeMethodsMap();
}
if (!node.methods![method]) {
node.methods![method] = [];
Expand Down
6 changes: 3 additions & 3 deletions test/bench/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ describe("benchmark", () => {
findAllRoutes();
`;
const { bytes, gzipSize } = await getBundleSize(code);
// console.log("bundle size", { bytes, gzipSize });
expect(bytes).toBeLessThanOrEqual(2500); // <2.5kb
expect(gzipSize).toBeLessThanOrEqual(1000); // <1kb
console.log("bundle size", { bytes, gzipSize });
expect(bytes).toBeLessThanOrEqual(3000); // <3kb
expect(gzipSize).toBeLessThanOrEqual(1500); // <1.5kb
});
});

Expand Down

0 comments on commit 2b7ac09

Please sign in to comment.