User Agents

Web browsers and everyday web tech, explained

Why Script Engines Don't Cache Dispatch Identifiers

· 6 min read · Cookies and Cache

In this article
  1. Background: What Dispatch Identifiers Are
  2. How Script Engines Actually Resolve Identifiers
  3. Why Caching the Identifier Itself Fails
  4. What Engines Do Instead: Shape-Based Caching
  5. Trade-Offs and Performance Considerations
  6. Common Misconception: "Caching Identifiers Would Be Faster"
  7. Frequently Asked Questions

Script engines do not cache dispatch identifiers because the dynamic nature of JavaScript and other scripting languages makes cached lookups frequently invalid, and the cost of maintaining cache coherence outweighs the performance benefit in most real-world workloads. Dispatch identifiers (property names, method names, or symbol keys) are resolved through a runtime lookup that must account for prototype chain mutations, object shape changes, and dynamic property addition or deletion—all of which can occur at any point during execution. While engines do use sophisticated inline caches (ICs) for property access, these caches store the result of a lookup (the value or method) keyed by the object's shape, not the identifier itself, because the identifier alone is not a stable key.

Background: What Dispatch Identifiers Are

In scripting languages like JavaScript, Python, and Ruby, a "dispatch identifier" is the name or key used to select a function or property at runtime. For example, in obj.method(), method is the dispatch identifier. Unlike compiled languages where method calls are resolved at compile time to a fixed address, script engines must resolve these identifiers at runtime because objects can change shape, prototypes can be altered, and new properties can be added dynamically. The engine's job is to find the property or method associated with that identifier in the object's own properties or along its prototype chain.

How Script Engines Actually Resolve Identifiers

Modern engines use a technique called inline caching (IC) to speed up property access. Instead of caching the identifier itself, they cache the result of the lookup—the property value or method—along with a check of the object's hidden class (or shape). For instance, in V8 (the JavaScript engine in Chrome and Node.js), each object has a hidden class that describes its layout. When code accesses obj.x, the engine stores the hidden class and the offset of x in that class. On subsequent accesses, it checks if the hidden class is the same; if so, it directly reads the value at the cached offset. This is not caching the identifier x; it's caching a fast path that is only valid as long as the object's shape remains identical.

Why Caching the Identifier Itself Fails

If an engine tried to cache a dispatch identifier globally (e.g., "the identifier foo always refers to this function"), it would break immediately because the same identifier can refer to different properties on different objects, and even on the same object at different times. Consider:

  • Two different objects may both have a method named draw, but they are different functions.
  • An object can have a property added or deleted, changing what draw resolves to.
  • Prototype chains can be modified at runtime, so obj.draw might resolve to different functions depending on when the lookup occurs.
  • In JavaScript, the with statement and eval can introduce entirely new scopes, making any cached identifier invalid.

Because of this, a cache keyed only by the identifier would be wrong most of the time. Engines would need to invalidate such caches on every mutation, which would be more expensive than simply doing the lookup.

What Engines Do Instead: Shape-Based Caching

Instead of caching identifiers, engines cache based on the shape (hidden class) of the object. This works because most objects in a given program have a stable shape for long periods. The engine maintains a global cache of shape-to-property-offset mappings. When code accesses obj.x, the engine does a quick check: does the object's current shape match the shape recorded in the inline cache? If yes, it reads the value at the precomputed offset. If no, it falls back to a slow path that does a full property lookup (including prototype chain traversal) and then updates the cache if the new shape is common.

This approach is highly effective because it avoids the cost of a full hash lookup on every access. The identifier itself is only used during the slow path to find the property in the object's property dictionary. Once the offset is known, the identifier is no longer needed.

Trade-Offs and Performance Considerations

There is a trade-off: shape-based caches consume memory and can be polluted by polymorphic call sites (where an object has multiple shapes). Engines limit the number of shapes tracked per call site (e.g., V8 uses a maximum of 4 shapes for monomorphic/polymorphic caches; beyond that, it goes megamorphic and uses a full hash lookup). But even in the megamorphic case, the engine does not cache the identifier; it uses a hash table of property names to offsets, which is still faster than a naive lookup but not as fast as a monomorphic inline cache.

Another reason identifier caching is not used: the identifier is often a string, and string comparison is expensive. Caching the identifier would require storing the string and comparing it on every access, which is slower than comparing a pointer to a hidden class. Hidden classes are just pointers, so checking them is a single pointer comparison.

Common Misconception: "Caching Identifiers Would Be Faster"

A common misconception is that if engines cached the final resolved function or property for a given identifier, they could skip the lookup entirely. That is exactly what inline caches do, but they key on the object's shape, not the identifier. The reason is that the identifier alone is not a valid key. For example, in JavaScript, obj.method and otherObj.method may have the same identifier but different functions. If you cached by identifier, you'd have to store a mapping from identifier to a list of (shape, result) pairs, which is essentially what a hash table does—but then you still have to check the shape to know which result applies. So the engine is already doing the optimal thing: it uses the shape as the primary key and the identifier only during the initial lookup.

Frequently Asked Questions

Why don't script engines just cache the method by name?

Because the same method name can refer to different functions on different objects, and even on the same object if the prototype changes. A cache keyed only by name would return stale results and require invalidation on every object mutation, which is more expensive than the lookup it would save.

What is the difference between an inline cache and a dispatch identifier cache?

An inline cache stores the result of a property lookup (the value or method) along with the object's shape. A dispatch identifier cache would store the result keyed only by the identifier string. Inline caches are safe because they verify the shape before using the cached result; identifier caches would be unsafe without shape checks.

Do all script engines use shape-based caching?

Most modern engines (V8, SpiderMonkey, JavaScriptCore, and others) use some form of shape or hidden class based caching. The specifics differ, but the principle is the same: they cache lookups based on the object's structure, not just the property name.

Can a developer manually cache dispatch results to speed up their code?

Yes, in some cases you can manually cache a method reference, e.g., const m = obj.method; m(); instead of obj.method(). This avoids the property lookup but only works if the method doesn't rely on this being bound to obj (or you use bind). However, engines already optimize this pattern, so the gain is often minimal.

Does caching identifiers cause memory leaks?

If an engine cached identifiers globally, it would need to hold references to the identifiers and their resolved values, which could prevent garbage collection of objects. Shape-based caches are also carefully managed to avoid leaks, but they are keyed by shapes which are themselves garbage-collected when no longer used.