From: weihaiyu <1600219867@qq.com> Date: Tue, 21 Jul 2020 13:24:58 +0000 (+0700) Subject: refactor(guards): optimize with one single for loop (#375) X-Git-Tag: v4.0.0-beta.3~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9273441e35e9177e5cb570a59267c6a1c8d2a83;p=thirdparty%2Fvuejs%2Frouter.git 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 --- 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