Cache snippet evaluations in the browser
This commit is contained in:
parent
30622a56ca
commit
95f71efdab
|
@ -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]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue