关于ECMA-262 Realm中[[TemplateMap]]存储方式及示例的技术问询
Great question! Let's unpack how [[TemplateMap]] handles template literals in ECMA-262—this is one of the more obscure internal slots, so it’s totally normal to need clarification.
What is [[TemplateMap]]?
First off, [[TemplateMap]] is an internal slot of a Realm record, built to cache parsed template literal data and their corresponding Template Objects. The core purpose here is reuse: if the same template literal is encountered multiple times in the same Realm, the engine doesn’t waste resources re-parsing it—it grabs the cached Template Object instead.
How Template Literals Get Stored in [[TemplateMap]]
When the engine processes a template literal (like `Hello ${name}!`), here’s the step-by-step flow for [[TemplateMap]]:
- It extracts two sets of string parts from the template:
- Cooked parts: The static sections after escape sequences (like
\n) are processed into their actual values. - Raw parts: The static sections as they appear in the source code (escape sequences remain unprocessed, e.g.,
\\ninstead of a newline).
- Cooked parts: The static sections after escape sequences (like
- It checks if the exact sequence of raw parts already exists as a key in [[TemplateMap]].
- If yes: Reuse the existing Template Object mapped to that key.
- If no: Create a new Template Object (with
cookedandrawproperties matching the extracted parts), store it in [[TemplateMap]] using the raw parts as the key, and use this object for the template’s evaluation.
Conceptual Example of [[TemplateMap]] Content
Since [[TemplateMap]] is an internal slot (you can’t access it directly from JavaScript), let’s walk through a conceptual example based on real code. Suppose we run these lines in a single Realm:
// First use of the greeting template const greetUser = `Hello ${currentUser}!`; // Second use of the SAME template structure (different substitution, same static parts) const greetAdmin = `Hello ${adminUser}!`; // A distinct template with different static parts const signOff = `Goodbye ${currentUser}!`; // A template with an escape sequence const multiLine = `Line 1\nLine 2`;
The [[TemplateMap]] for this Realm would conceptually look like this (keys are raw part sequences, values are Template Objects):
- Key:
["Hello ", "!"]- Value: Template Object:
{ cooked: ["Hello ", "!"], // No escape sequences here, so matches raw raw: ["Hello ", "!"] }
- Value: Template Object:
- Key:
["Goodbye ", "!"]- Value: Template Object:
{ cooked: ["Goodbye ", "!"], raw: ["Goodbye ", "!"] }
- Value: Template Object:
- Key:
["Line 1\\nLine 2"]- Value: Template Object:
{ cooked: ["Line 1\nLine 2"], // Escape sequence processed into a newline raw: ["Line 1\\nLine 2"] // Raw source code (backslash + 'n') }
- Value: Template Object:
Key Takeaways
- [[TemplateMap]] is per-Realm: Templates in separate Realms (like iframes or web workers) don’t share cached Template Objects.
- Caching is strict: Even a tiny difference in the raw static parts (e.g., an extra space) will create a new [[TemplateMap]] entry.
- It’s purely internal: There’s no way to interact with [[TemplateMap]] directly via JavaScript—this is strictly a spec-defined engine optimization.
内容的提问来源于stack exchange,提问作者ENvironmentSet

