]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
perf: use getter instead of `computed` for route location (#1916)
authorAnthony Fu <anthonyfu117@hotmail.com>
Wed, 5 Jul 2023 12:42:22 +0000 (14:42 +0200)
committerGitHub <noreply@github.com>
Wed, 5 Jul 2023 12:42:22 +0000 (14:42 +0200)
packages/router/__tests__/mount.ts
packages/router/src/router.ts

index fa8888d4ae0c69708cfcf26284ef4051be721513..aa9ef8e9ec0b0e75d54808c2a0ca7531d266a4a9 100644 (file)
@@ -1,4 +1,4 @@
-import { reactive, nextTick, ComputedRef, computed, shallowRef } from 'vue'
+import { nextTick, shallowRef, shallowReactive } from 'vue'
 import { RouteLocationNormalizedLoose } from './utils'
 import {
   routeLocationKey,
@@ -9,12 +9,6 @@ import { RouteLocationNormalized } from '../src'
 export function createMockedRoute(
   initialValue: RouteLocationNormalizedLoose | RouteLocationNormalized
 ) {
-  const route = {} as {
-    [k in keyof RouteLocationNormalizedLoose]: ComputedRef<
-      RouteLocationNormalizedLoose[k]
-    >
-  }
-
   const routeRef = shallowRef<
     RouteLocationNormalized | RouteLocationNormalizedLoose
   >(initialValue)
@@ -26,14 +20,16 @@ export function createMockedRoute(
     return nextTick()
   }
 
+  const route = {} as RouteLocationNormalizedLoose
+
   for (let key in initialValue) {
-    // @ts-expect-error
-    route[key] =
-      // new line to still get errors here
-      computed(() => routeRef.value[key as keyof RouteLocationNormalizedLoose])
+    Object.defineProperty(route, key, {
+      enumerable: true,
+      get: () => routeRef.value[key as keyof RouteLocationNormalizedLoose],
+    })
   }
 
-  const value = reactive(route)
+  const value = shallowReactive(route)
 
   return {
     value,
index f7ad75d0388bc4cefa8becd5dddf11528d198d6f..dbd426f32351b4d467d58c979ea0f10f0cf08921 100644 (file)
@@ -41,16 +41,7 @@ import {
   stringifyQuery as originalStringifyQuery,
   LocationQuery,
 } from './query'
-import {
-  shallowRef,
-  Ref,
-  nextTick,
-  App,
-  ComputedRef,
-  reactive,
-  unref,
-  computed,
-} from 'vue'
+import { shallowRef, Ref, nextTick, App, unref, shallowReactive } from 'vue'
 import { RouteRecord, RouteRecordNormalized } from './matcher/types'
 import {
   parseURL,
@@ -1235,18 +1226,16 @@ export function createRouter(options: RouterOptions): Router {
         })
       }
 
-      const reactiveRoute = {} as {
-        [k in keyof RouteLocationNormalizedLoaded]: ComputedRef<
-          RouteLocationNormalizedLoaded[k]
-        >
-      }
+      const reactiveRoute = {} as RouteLocationNormalizedLoaded
       for (const key in START_LOCATION_NORMALIZED) {
-        // @ts-expect-error: the key matches
-        reactiveRoute[key] = computed(() => currentRoute.value[key])
+        Object.defineProperty(reactiveRoute, key, {
+          get: () => currentRoute.value[key as keyof RouteLocationNormalized],
+          enumerable: true,
+        })
       }
 
       app.provide(routerKey, router)
-      app.provide(routeLocationKey, reactive(reactiveRoute))
+      app.provide(routeLocationKey, shallowReactive(reactiveRoute))
       app.provide(routerViewLocationKey, currentRoute)
 
       const unmountApp = app.unmount