From c9273441e35e9177e5cb570a59267c6a1c8d2a83 Mon Sep 17 00:00:00 2001 From: weihaiyu <1600219867@qq.com> Date: Tue, 21 Jul 2020 20:24:58 +0700 Subject: [PATCH] refactor(guards): optimize with one single for loop (#375) * 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 | 22 +++++++++++++--------- src/scrollBehavior.ts | 6 +----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/router.ts b/src/router.ts index c1d8d804..9447208d 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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] diff --git a/src/scrollBehavior.ts b/src/scrollBehavior.ts index 066b2f87..4d955cec 100644 --- a/src/scrollBehavior.ts +++ b/src/scrollBehavior.ts @@ -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 -- 2.47.3