Skip to content

Commit

Permalink
lib: cache process.binding(…) wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 7, 2021
1 parent 6986fa0 commit da65d8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/internal/legacy/processbinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ObjectFromEntries,
ObjectEntries,
SafeArrayIterator,
} = primordials;
const { types } = require('util');
const types = require('internal/util/types');

module.exports = {
const factories = {
util() {
return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter(
ObjectEntries(types),
Expand All @@ -34,3 +35,17 @@ module.exports = {
})));
}
};

module.exports = ObjectFromEntries(
new SafeArrayIterator(
ArrayPrototypeMap(ObjectEntries(factories), ({ 0: key, 1: factory }) => {
let result;
return [key, () => {
if (!result) {
result = factory();
}
return result;
}];
}),
),
);
2 changes: 2 additions & 0 deletions test/parallel/test-process-binding-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ assert.deepStrictEqual(
for (const k of Object.keys(utilBinding)) {
assert.strictEqual(utilBinding[k], util.types[k]);
}

assert.strictEqual(utilBinding, process.binding('util'));

0 comments on commit da65d8a

Please sign in to comment.