performance: load action sources lazily on container
- big improvement for larger actions with many npm modules, from ~9 sec to ~4sec startup
diff --git a/src/kinds/nodejs/mount-plain.js b/src/kinds/nodejs/mount-plain.js
index 1f4557e..9298bf0 100644
--- a/src/kinds/nodejs/mount-plain.js
+++ b/src/kinds/nodejs/mount-plain.js
@@ -41,9 +41,6 @@
return fn;
}
-// load and validate on /init for quick feedback
-load(path);
-
// eslint-disable-next-line no-unused-vars
function main(args) { // lgtm [js/unused-local-variable]
// load code again on every new invocation
diff --git a/src/kinds/nodejs/mount-require.js b/src/kinds/nodejs/mount-require.js
index 44be9f8..6c4cbbe 100644
--- a/src/kinds/nodejs/mount-require.js
+++ b/src/kinds/nodejs/mount-require.js
@@ -26,24 +26,34 @@
// name of module file (for helpful errors)
const sourceFile = "$$sourceFile$$";
-// load and validate on /init for quick feedback
-try {
- require(path);
-} catch (e) {
- throw `Cannot load module '${sourceFile}': ${e}`;
-}
-if (typeof require(path)[mainFn] !== "function") {
- throw `'${mainFn}' is not a function in '${sourceFile}'. Specify the right function in wskdebug using --main.`;
-}
-
function clearEntireRequireCache() {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
}
+let firstRun = true;
+
// eslint-disable-next-line no-unused-vars
function main(args) { // lgtm [js/unused-local-variable]
+
+ if (firstRun) {
+ const start = Date.now();
+ console.log(`[wskdebug] loading action sources from ${sourceFile}`);
+ // validation with better error messages
+ try {
+ require(path);
+ } catch (e) {
+ throw `Cannot load module '${sourceFile}': ${e}`;
+ }
+
+ if (typeof require(path)[mainFn] !== "function") {
+ throw `'${mainFn}' is not a function in '${sourceFile}'. Specify the right function in wskdebug using --main.`;
+ }
+ firstRun = false;
+ console.log(`[wskdebug] loaded in ${Date.now() - start} ms.`);
+ }
+
// force reload of entire mounted action on every invocation
clearEntireRequireCache();