Skip to content

Commit f1b839e

Browse files
authored
perf: use getter instead of computed for route location (#1916)
1 parent c5b1ea1 commit f1b839e

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

packages/router/__tests__/mount.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive, nextTick, ComputedRef, computed, shallowRef } from 'vue'
1+
import { nextTick, shallowRef, shallowReactive } from 'vue'
22
import { RouteLocationNormalizedLoose } from './utils'
33
import {
44
routeLocationKey,
@@ -9,12 +9,6 @@ import { RouteLocationNormalized } from '../src'
99
export function createMockedRoute(
1010
initialValue: RouteLocationNormalizedLoose | RouteLocationNormalized
1111
) {
12-
const route = {} as {
13-
[k in keyof RouteLocationNormalizedLoose]: ComputedRef<
14-
RouteLocationNormalizedLoose[k]
15-
>
16-
}
17-
1812
const routeRef = shallowRef<
1913
RouteLocationNormalized | RouteLocationNormalizedLoose
2014
>(initialValue)
@@ -26,14 +20,16 @@ export function createMockedRoute(
2620
return nextTick()
2721
}
2822

23+
const route = {} as RouteLocationNormalizedLoose
24+
2925
for (let key in initialValue) {
30-
// @ts-expect-error
31-
route[key] =
32-
// new line to still get errors here
33-
computed(() => routeRef.value[key as keyof RouteLocationNormalizedLoose])
26+
Object.defineProperty(route, key, {
27+
enumerable: true,
28+
get: () => routeRef.value[key as keyof RouteLocationNormalizedLoose],
29+
})
3430
}
3531

36-
const value = reactive(route)
32+
const value = shallowReactive(route)
3733

3834
return {
3935
value,

packages/router/src/router.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ import {
4141
stringifyQuery as originalStringifyQuery,
4242
LocationQuery,
4343
} from './query'
44-
import {
45-
shallowRef,
46-
Ref,
47-
nextTick,
48-
App,
49-
ComputedRef,
50-
reactive,
51-
unref,
52-
computed,
53-
} from 'vue'
44+
import { shallowRef, Ref, nextTick, App, unref, shallowReactive } from 'vue'
5445
import { RouteRecord, RouteRecordNormalized } from './matcher/types'
5546
import {
5647
parseURL,
@@ -1235,18 +1226,16 @@ export function createRouter(options: RouterOptions): Router {
12351226
})
12361227
}
12371228

1238-
const reactiveRoute = {} as {
1239-
[k in keyof RouteLocationNormalizedLoaded]: ComputedRef<
1240-
RouteLocationNormalizedLoaded[k]
1241-
>
1242-
}
1229+
const reactiveRoute = {} as RouteLocationNormalizedLoaded
12431230
for (const key in START_LOCATION_NORMALIZED) {
1244-
// @ts-expect-error: the key matches
1245-
reactiveRoute[key] = computed(() => currentRoute.value[key])
1231+
Object.defineProperty(reactiveRoute, key, {
1232+
get: () => currentRoute.value[key as keyof RouteLocationNormalized],
1233+
enumerable: true,
1234+
})
12461235
}
12471236

12481237
app.provide(routerKey, router)
1249-
app.provide(routeLocationKey, reactive(reactiveRoute))
1238+
app.provide(routeLocationKey, shallowReactive(reactiveRoute))
12501239
app.provide(routerViewLocationKey, currentRoute)
12511240

12521241
const unmountApp = app.unmount

0 commit comments

Comments
 (0)