Skip to content

Commit 465e7d6

Browse files
authored
Look up on productMap keys as a set (#25590)
1 parent 16113fd commit 465e7d6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/page.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import getLinkData from './get-link-data.js'
1818
import getDocumentType from './get-document-type.js'
1919
import { union } from 'lodash-es'
2020

21+
// We're going to check a lot of pages' "ID" (the first part of
22+
// the relativePath) against `productMap` to make sure it's valid.
23+
// To avoid having to do `Object.keys(productMap).includes(id)`
24+
// every single time, we turn it into a Set once.
25+
const productMapKeysAsSet = new Set(Object.keys(productMap))
26+
2127
// Wrapper on renderContent() that caches the output depending on the
2228
// `context` by extracting information about the page's current permalink
2329
const _renderContentCache = new Map()
@@ -162,10 +168,7 @@ class Page {
162168

163169
// make sure the ID is valid
164170
if (process.env.NODE_ENV !== 'test') {
165-
assert(
166-
Object.keys(productMap).includes(id),
167-
`page ${this.fullPath} has an invalid product ID: ${id}`
168-
)
171+
assert(productMapKeysAsSet.has(id), `page ${this.fullPath} has an invalid product ID: ${id}`)
169172
}
170173

171174
return id

0 commit comments

Comments
 (0)