Skip to content

Commit b2c15cd

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

2 files changed

Lines changed: 6 additions & 17 deletions

File tree

lib/internal/bootstrap/loaders.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ const legacyWrapperList = new SafeSet([
136136
'DEP0111');
137137
}
138138
if (legacyWrapperList.has(module)) {
139-
return nativeModuleRequire('internal/legacy/processbinding')[module]();
139+
return bindingObj[module] ??= nativeModuleRequire('internal/legacy/processbinding')[module]();
140140
}
141141
return internalBinding(module);
142142
}
143143
// eslint-disable-next-line no-restricted-syntax
144144
throw new Error(`No such module: ${module}`);
145145
};
146+
}
147+
148+
{
149+
const bindingObj = ObjectCreate(null);
146150

147151
process._linkedBinding = function _linkedBinding(module) {
148152
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)