Cache snippet evaluations in the browser

This commit is contained in:
Andrew Kingston 2024-03-13 12:52:36 +00:00
parent 30622a56ca
commit 95f71efdab
1 changed files with 7 additions and 2 deletions

View File

@ -51,8 +51,10 @@ module.exports.processJS = (handlebars, context) => {
// This is required to allow the final `return` statement to be valid. // This is required to allow the final `return` statement to be valid.
const js = iifeWrapper(atob(handlebars)) const js = iifeWrapper(atob(handlebars))
// Transform snippets into an object for faster access // Transform snippets into an object for faster access, and cache previously
// evaluated snippets
let snippetMap = {} let snippetMap = {}
let snippetCache = {}
for (let snippet of context.snippets || []) { for (let snippet of context.snippets || []) {
snippetMap[snippet.name] = snippet.code snippetMap[snippet.name] = snippet.code
} }
@ -70,7 +72,10 @@ module.exports.processJS = (handlebars, context) => {
{}, {},
{ {
get: function (_, name) { get: function (_, name) {
return eval(iifeWrapper(snippetMap[name])) if (!(name in snippetCache)) {
snippetCache[name] = eval(iifeWrapper(snippetMap[name]))
}
return snippetCache[name]
}, },
} }
), ),