Skip to content

Commit da65d8a

Browse files
committed
lib: cache process.binding(…) wrappers
Refs: nodejs#37819
1 parent 6986fa0 commit da65d8a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/internal/legacy/processbinding.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
const {
33
ArrayPrototypeFilter,
44
ArrayPrototypeIncludes,
5+
ArrayPrototypeMap,
56
ObjectFromEntries,
67
ObjectEntries,
78
SafeArrayIterator,
89
} = primordials;
9-
const { types } = require('util');
10+
const types = require('internal/util/types');
1011

11-
module.exports = {
12+
const factories = {
1213
util() {
1314
return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter(
1415
ObjectEntries(types),
@@ -34,3 +35,17 @@ module.exports = {
3435
})));
3536
}
3637
};
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+
);

test/parallel/test-process-binding-util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ assert.deepStrictEqual(
2828
for (const k of Object.keys(utilBinding)) {
2929
assert.strictEqual(utilBinding[k], util.types[k]);
3030
}
31+
32+
assert.strictEqual(utilBinding, process.binding('util'));

0 commit comments

Comments
 (0)