Sub topic 7
Dictionary Mode: When Objects Fall Off the Fast Path
This chapter assumes you know how hidden classes describe an object's layout and how adding properties creates shape transitions. You should also be able to run Node.js with extra flags from the command line.
Two Ways to Store Properties
V8 can store an object's named properties in one of two ways.
- Fast mode is the default. The object's hidden class (its map) records which property sits at which slot. Code that reads
o.xcan compute a fixed offset and load the value directly. - Dictionary mode (also called slow mode) stores properties in a hash table attached to the object. The table holds each key, value, and attribute set. Every lookup hashes the key and searches the table.
Dictionary mode is not a bug. It works better for objects whose keys change all the time, because transitions are never created for them and the transition tree does not grow. The cost is that the engine can no longer find a property by reading a known offset.
What Triggers the Switch
The most common trigger is delete. If you delete the property that was added most recently, V8 can usually step back along the transition chain to the previous hidden class. If you delete any other property, no existing hidden class describes the result, so V8 usually converts the object to dictionary mode.
// Run with: node --allow-natives-syntax delete.js
const last = { a: 1, b: 2 };
delete last.b; // b was the last property added
console.log(%HasFastProperties(last)); // typically true
const middle = { a: 1, b: 2 };
delete middle.a; // a was not the last property added
console.log(%HasFastProperties(middle)); // false
const cleared = { a: 1, b: 2 };
cleared.a = undefined; // shape unchanged
console.log(%HasFastProperties(cleared)); // trueUsing a plain object as a hash map can also trigger dictionary mode. This happens when the code adds many computed keys, such as user IDs or URL paths. Past an internal limit on how many properties a map can describe, V8 stops creating transitions and switches the object to a dictionary.
Why It Is Slower
Inline caches, covered in the next chapter, work by remembering "objects with this map keep x at this offset." A dictionary-mode object has no stable offsets to remember, so property access at that call site takes a slower, generic path. The difference is small for one access but adds up in hot loops.
Common Mistakes and Fixes
| Pattern | Effect | Better choice |
|---|---|---|
| `delete obj.field` to clear a value | Usually dictionary mode | Assign `undefined` or `null` |
| Plain object keyed by arbitrary IDs | Dictionary mode, plus prototype key risks | Use a `Map` |
| Removing a field before passing an object on | Shape change at every call | Build a new object with only the needed fields |
For data with keys that change, Map is designed for frequent insertions and deletions:
const sessions = new Map();
sessions.set('u42', { started: Date.now() });
sessions.delete('u42');
console.log(sessions.size); // 0Exercise
A config object { host, port, debug } has delete config.host called on it before it is passed to a hot function. What happens to its properties, and what is a shape-stable alternative?
Show answer
Because host was not the last property added, V8 usually converts the object to dictionary mode, so property reads inside the hot function go through a hash lookup. Set config.host = undefined instead, or create a new object such as { port: config.port, debug: config.debug } so it keeps fast properties.
Exercise
Why does deleting the most recently added property often keep an object in fast mode?
Show answer
The previous hidden class already exists as the parent in the transition chain, so V8 can move the object back to that map without creating a new layout.