From 4300e088856acb5ba3af9b66e27998629a89c5dc Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 26 Jul 2025 21:27:56 +1000 Subject: [PATCH] Add script caching --- worker.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/worker.ts b/worker.ts index 467ff4f..eea83f5 100644 --- a/worker.ts +++ b/worker.ts @@ -1,4 +1,6 @@ // isolator/worker.ts +const moduleCache = new Map(); + const States = Object.freeze({ SUCCESS: "SUCCESS", NOT_A_FUNCTION: "NOT_A_FUNCTION", @@ -50,11 +52,15 @@ self.onmessage = async (e) => { status ); - // Use a data URL to import the user's code as an ES module. - const dataUrl = `data:text/javascript;base64,${btoa( - unescape(encodeURIComponent(code)) - )}`; - const userModule = await import(dataUrl); + let userModule = moduleCache.get(code); + if (!userModule) { + // Use a data URL to import the user's code as an ES module. + const dataUrl = `data:text/javascript;base64,${btoa( + unescape(encodeURIComponent(code)) + )}`; + userModule = await import(dataUrl); + moduleCache.set(code, userModule); + } if (typeof userModule.default !== "function") { throw new Error(