From 2b82e533dbaf9e6048351aa29bcef64063c1e50d Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 21:43:53 +0800 Subject: [PATCH] optimize: use map instead of object --- packages/docusaurus/src/client/normalizeLocation.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 37c55b4000f0..74192a451c08 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -10,13 +10,13 @@ import routes from '@generated/routes'; import type {Location} from 'history'; // Memoize previously normalized pathnames. -const pathnames: {[rawPathname: string]: string} = {}; +const pathnames = new Map(); export default function normalizeLocation(location: T): T { - if (pathnames[location.pathname]) { + if (pathnames.has(location.pathname)) { return { ...location, - pathname: pathnames[location.pathname], + pathname: pathnames.get(location.pathname), }; } @@ -24,14 +24,14 @@ export default function normalizeLocation(location: T): T { // away, or it will render to a 404 page. const matchedRoutes = matchRoutes(routes, location.pathname); if (matchedRoutes.some(({route}) => route.exact === true)) { - pathnames[location.pathname] = location.pathname; + pathnames.set(location.pathname, location.pathname); return location; } const pathname = location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; - pathnames[location.pathname] = pathname; + pathnames.set(location.pathname, pathname); return { ...location,