From 3499d5eb8ff4ec60ee635afbc491f9620502a06a Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Thu, 4 Nov 2021 20:50:58 -0400 Subject: [PATCH] avoid some path.posix.join with simple caching (#22614) --- lib/create-tree.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/create-tree.js b/lib/create-tree.js index eec6f240a80d..0a5e47711286 100644 --- a/lib/create-tree.js +++ b/lib/create-tree.js @@ -4,10 +4,22 @@ import fs from 'fs/promises' import Page from './page.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) +// Module level cache +const _basePaths = new Map() +// Return a full directory based on __dirname from a specific language directory. +// This function is memoized with a simple global cache object. +function getBasePath(directory) { + if (!_basePaths.has(directory)) { + _basePaths.set(directory, path.posix.join(__dirname, '..', directory, 'content')) + console.log(_basePaths.get(directory)) + } + return _basePaths.get(directory) +} + export default async function createTree(originalPath, langObj) { // This basePath definition is needed both here and in lib/page-data.js because this // function runs recursively, and the value for originalPath changes on recursive runs. - const basePath = path.posix.join(__dirname, '..', langObj.dir, 'content') + const basePath = getBasePath(langObj.dir) // On recursive runs, this is processing page.children items in `/` format. // If the path exists as is, assume this is a directory with a child index.md.