Skip to content

Commit dd1e3e3

Browse files
committed
faster
1 parent 627e017 commit dd1e3e3

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

packages/router-core/src/new-process-route-tree.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ function parseSegments<TRouteLike extends RouteLike>(data: Uint16Array, route: T
142142
nextNode = existingNode
143143
} else {
144144
node.static ??= new Map()
145-
nextNode = {}
145+
nextNode = createEmptyNode()
146+
nextNode.parent = node
146147
node.static.set(value, nextNode)
147148
}
148149
} else {
@@ -152,7 +153,8 @@ function parseSegments<TRouteLike extends RouteLike>(data: Uint16Array, route: T
152153
nextNode = existingNode
153154
} else {
154155
node.staticInsensitive ??= new Map()
155-
nextNode = {}
156+
nextNode = createEmptyNode()
157+
nextNode.parent = node
156158
node.staticInsensitive.set(name, nextNode)
157159
}
158160
}
@@ -167,7 +169,8 @@ function parseSegments<TRouteLike extends RouteLike>(data: Uint16Array, route: T
167169
if (existingNode) {
168170
nextNode = existingNode.node
169171
} else {
170-
nextNode = {}
172+
nextNode = createEmptyNode()
173+
nextNode.parent = node
171174
node.dynamic ??= []
172175
node.dynamic.push({
173176
name: value,
@@ -188,7 +191,8 @@ function parseSegments<TRouteLike extends RouteLike>(data: Uint16Array, route: T
188191
if (existingNode) {
189192
nextNode = existingNode.node
190193
} else {
191-
nextNode = {}
194+
nextNode = createEmptyNode()
195+
nextNode.parent = node
192196
node.optional ??= []
193197
node.optional.push({
194198
name: value,
@@ -260,39 +264,52 @@ function sortTreeNodes(node: SegmentNode) {
260264

261265
type SegmentNode = {
262266
// Static segments (highest priority)
263-
static?: Map<string, SegmentNode>
267+
static: Map<string, SegmentNode> | null
264268

265269
// Case insensitive static segments (second highest priority)
266-
staticInsensitive?: Map<string, SegmentNode>
270+
staticInsensitive: Map<string, SegmentNode> | null
267271

268272
// Dynamic segments ($param)
269-
dynamic?: Array<{
273+
dynamic: Array<{
270274
name: string
271-
prefix?: string
272-
suffix?: string
275+
prefix: string | undefined
276+
suffix: string | undefined
273277
caseSensitive: boolean
274278
node: SegmentNode
275-
}>
279+
}> | null
276280

277281
// Optional dynamic segments ({-$param})
278-
optional?: Array<{
282+
optional: Array<{
279283
name: string
280-
prefix?: string
281-
suffix?: string
284+
prefix: string | undefined
285+
suffix: string | undefined
282286
caseSensitive: boolean
283287
node: SegmentNode
284-
}>
288+
}> | null
285289

286290
// Wildcard segment ($ - lowest priority)
287-
wildcard?: {
288-
prefix?: string
289-
suffix?: string
290-
}
291+
wildcard: {
292+
prefix: string | undefined
293+
suffix: string | undefined
294+
} | null
291295

292296
// Terminal route (if this path can end here)
293-
routeId?: string
297+
routeId: string | null
298+
299+
parent: SegmentNode | null
294300
}
295301

302+
function createEmptyNode(): SegmentNode {
303+
return {
304+
static: null,
305+
staticInsensitive: null,
306+
dynamic: null,
307+
optional: null,
308+
wildcard: null,
309+
routeId: null,
310+
parent: null
311+
}
312+
}
296313

297314
// function intoRouteLike(routeTree, parent) {
298315
// const route = {
@@ -325,7 +342,7 @@ export function processRouteTree<TRouteLike extends RouteLike>({
325342
routeTree: TRouteLike
326343
initRoute?: (route: TRouteLike, index: number) => void
327344
}) {
328-
const segmentTree: SegmentNode = {}
345+
const segmentTree = createEmptyNode()
329346
const data = new Uint16Array(6)
330347
const routesById = {} as Record<string, TRouteLike>
331348
const routesByPath = {} as Record<string, TRouteLike>
@@ -342,4 +359,4 @@ export function processRouteTree<TRouteLike extends RouteLike>({
342359
routesById,
343360
routesByPath,
344361
}
345-
}
362+
}

0 commit comments

Comments
 (0)