Skip to content

Commit b57a517

Browse files
committed
lib: cache process.binding(…) wrappers in loaders.js
1 parent da65d8a commit b57a517

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

lib/internal/bootstrap/loaders.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,18 @@ const legacyWrapperList = new SafeSet([
136136
'DEP0111');
137137
}
138138
if (legacyWrapperList.has(module)) {
139-
return nativeModuleRequire('internal/legacy/processbinding')[module]();
139+
return bindingObj[module] ??=
140+
nativeModuleRequire('internal/legacy/processbinding')[module]();
140141
}
141142
return internalBinding(module);
142143
}
143144
// eslint-disable-next-line no-restricted-syntax
144145
throw new Error(`No such module: ${module}`);
145146
};
147+
}
148+
149+
{
150+
const bindingObj = ObjectCreate(null);
146151

147152
process._linkedBinding = function _linkedBinding(module) {
148153
module = String(module);

lib/internal/legacy/processbinding.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
const {
33
ArrayPrototypeFilter,
44
ArrayPrototypeIncludes,
5-
ArrayPrototypeMap,
65
ObjectFromEntries,
76
ObjectEntries,
87
SafeArrayIterator,
98
} = primordials;
109
const types = require('internal/util/types');
1110

12-
const factories = {
11+
module.exports = {
1312
util() {
1413
return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter(
1514
ObjectEntries(types),
@@ -35,17 +34,3 @@ const factories = {
3534
})));
3635
}
3736
};
38-
39-
module.exports = ObjectFromEntries(
40-
new SafeArrayIterator(
41-
ArrayPrototypeMap(ObjectEntries(factories), ({ 0: key, 1: factory }) => {
42-
let result;
43-
return [key, () => {
44-
if (!result) {
45-
result = factory();
46-
}
47-
return result;
48-
}];
49-
}),
50-
),
51-
);

0 commit comments

Comments
 (0)