]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(guards): optimize with one single for loop (#375)
authorweihaiyu <1600219867@qq.com>
Tue, 21 Jul 2020 13:24:58 +0000 (20:24 +0700)
committerGitHub <noreply@github.com>
Tue, 21 Jul 2020 13:24:58 +0000 (15:24 +0200)
* refactor(src): optimized with one single for loop

* refactor(src): optimized with one single for loop

* fix(src): The _RouteLocationBase define but never be used

src/router.ts
src/scrollBehavior.ts

index c1d8d804e39168eda4d56799a87829d885ee72b5..9447208dab50cdf70d1be0e0cd264ba45b4fbbd5 100644 (file)
@@ -985,15 +985,19 @@ function extractChangingRecords(
   const updatingRecords: RouteRecordNormalized[] = []
   const enteringRecords: RouteRecordNormalized[] = []
 
-  // TODO: could be optimized with one single for loop
-  for (const record of from.matched) {
-    if (to.matched.indexOf(record) < 0) leavingRecords.push(record)
-    else updatingRecords.push(record)
-  }
-
-  for (const record of to.matched) {
-    // the type doesn't matter because we are comparing per reference
-    if (from.matched.indexOf(record as any) < 0) enteringRecords.push(record)
+  const len = Math.max(from.matched.length, to.matched.length)
+  for (let i = 0; i < len; i++) {
+    const recordFrom = from.matched[i]
+    if (recordFrom) {
+      if (to.matched.indexOf(recordFrom) < 0) leavingRecords.push(recordFrom)
+      else updatingRecords.push(recordFrom)
+    }
+    const recordTo = to.matched[i]
+    if (recordTo) {
+      // the type doesn't matter because we are comparing per reference
+      if (from.matched.indexOf(recordTo as any) < 0)
+        enteringRecords.push(recordTo)
+    }
   }
 
   return [leavingRecords, updatingRecords, enteringRecords]
index 066b2f879e50f16417df96b0fb4c22f6a167de62..4d955cec5de713d5ed480295ca9db32e1ccf2d72 100644 (file)
@@ -1,8 +1,4 @@
-import {
-  RouteLocationNormalized,
-  RouteLocationNormalizedLoaded,
-  _RouteLocationBase,
-} from './types'
+import { RouteLocationNormalized, RouteLocationNormalizedLoaded } from './types'
 import { warn } from './warning'
 
 // we use types instead of interfaces to make it work with HistoryStateValue type