Skip to content

Commit 41cbc53

Browse files
authored
perf: delay populating node-globals (#8506)
1 parent 3be0986 commit 41cbc53

File tree

1 file changed

+20
-10
lines changed
  • packages/vitest/src/integrations/env

1 file changed

+20
-10
lines changed

packages/vitest/src/integrations/env/node.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,40 @@ const denyList = new Set([
1111
'Uint8Array',
1212
])
1313

14-
const nodeGlobals = new Map(
15-
Object.getOwnPropertyNames(globalThis)
16-
.filter(global => !denyList.has(global))
17-
.map((nodeGlobalsKey) => {
14+
const nodeGlobals = new Map()
15+
16+
function populateNodeGlobals() {
17+
if (nodeGlobals.size !== 0) {
18+
return
19+
}
20+
21+
const names = Object.getOwnPropertyNames(globalThis)
22+
const length = names.length
23+
for (let i = 0; i < length; i++) {
24+
const globalName = names[i]
25+
if (!denyList.has(globalName)) {
1826
const descriptor = Object.getOwnPropertyDescriptor(
1927
globalThis,
20-
nodeGlobalsKey,
28+
globalName,
2129
)
2230

2331
if (!descriptor) {
2432
throw new Error(
25-
`No property descriptor for ${nodeGlobalsKey}, this is a bug in Vitest.`,
33+
`No property descriptor for ${globalName}, this is a bug in Vitest.`,
2634
)
2735
}
28-
29-
return [nodeGlobalsKey, descriptor]
30-
}),
31-
)
36+
nodeGlobals.set(globalName, descriptor)
37+
}
38+
}
39+
}
3240

3341
export default <Environment>{
3442
name: 'node',
3543
viteEnvironment: 'ssr',
3644
// this is largely copied from jest's node environment
3745
async setupVM() {
46+
populateNodeGlobals()
47+
3848
const vm = await import('node:vm')
3949
let context = vm.createContext()
4050
let global = vm.runInContext('this', context)

0 commit comments

Comments
 (0)