while (c) {
tag`str0 ${e} str1`
}
The JavaScript runtime creates a frozen array like Object.freeze(['str0 ', ' str1'])
but with an additional .raw
property.
Is it okay to use that object as a key in a WeakMap
to avoid having to redo work based on the array each time through the loop?
const memoTable = new WeakMap
function tag(templateStrings, ...values) {
let cached = memoTable.get(templateStrings)
if (!cached) {
// Compute cached and put it in the table for next time.
}
// Do something with cached and values
}
Section 12.2.9.3 Runtime Semantics: GetTemplateObject ( templateLiteral ) describes how this value is cached:
- Let realm be the current Realm Record.
- Let templateRegistry be realm.[[TemplateMap]].
so it should be the same from use to use of tag
in the loop above which would be a nice property for a key to have.
It seems to me that the [[TemplateMap]] would have to weakly reference the template object array because otherwise
for (let i = 0; i < 1e6; ++i) {
eval('(() => {})`' + i + '`');
}
would leak memory.
I don't see anything in the specification but is it the case for widely used JavaScript engines that WeakMap entries for tagged string template uses not in re-enterable scopes will eventually be collected?
I ask because I've implemented something based on this assumption but haven't figured out how to test it.