Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap: optimize modules loaded in the built-in snapshot #45849

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lib: add getLazy() method to internal/util
This patch adds a getLazy() method to facilitate initialize-once
lazy loading in the internals.
  • Loading branch information
joyeecheung committed Dec 13, 2022
commit 9c65244a45164ec451da294f85c4ded225e52f94
18 changes: 18 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,25 @@ function isArrayBufferDetached(value) {
return false;
}

/**
* @template T return value of loader
* @param {()=>T} loader
* @returns {()=>T}
*/
function getLazy(loader) {
let value;
let initialized = false;
return function() {
if (initialized === false) {
value = loader();
initialized = true;
}
return value;
};
}

module.exports = {
getLazy,
assertCrypto,
cachedResult,
convertToValidSignal,
Expand Down