You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于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.

Understanding [[TemplateMap]] in ECMA-262

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]]:

  1. 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., \\n instead of a newline).
  2. 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 cooked and raw properties 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 ", "!"]
      }
      
  • Key: ["Goodbye ", "!"]
    • Value: Template Object:
      {
        cooked: ["Goodbye ", "!"],
        raw: ["Goodbye ", "!"]
      }
      
  • 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')
      }
      

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:32:04