1
0
Fork 0
mirror of synced 2024-10-02 10:08:09 +13:00

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

View file

@ -51,8 +51,10 @@ module.exports.processJS = (handlebars, context) => {
// This is required to allow the final `return` statement to be valid.
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 snippetCache = {}
for (let snippet of context.snippets || []) {
snippetMap[snippet.name] = snippet.code
}
@ -70,7 +72,10 @@ module.exports.processJS = (handlebars, context) => {
{},
{
get: function (_, name) {
return eval(iifeWrapper(snippetMap[name]))
if (!(name in snippetCache)) {
snippetCache[name] = eval(iifeWrapper(snippetMap[name]))
}
return snippetCache[name]
},
}
),