Sub topic 7
Dictionary Mode: When Objects Fall Off the Fast Path
This chapter assumes you have read the previous chapters on hidden classes and shape transitions. You should know that V8 calls a hidden class a map, and that adding a property moves an object along a transition chain from one map to the next. You will also need Node.js on your machine to run the examples with --allow-natives-syntax.
Hidden classes work well when objects keep a stable, predictable shape. Some code does not keep its objects stable. It deletes properties, adds hundreds of keys at runtime, or uses a plain object as a lookup table. For those objects V8 has a second representation: dictionary mode, which also goes by the names slow properties and hash table mode. This chapter covers what dictionary mode is, what triggers it, what it costs, and when the cost actually matters.
Two Ways to Store Properties
In fast mode, an object's map holds a descriptor array. This array lists each property name, its attributes, and the slot where the value lives. The object itself holds only the values: some sit directly inside the object (in-object properties) and the rest sit in a separate backing array. Because the map fixes the layout, the engine can say "property x is at offset 2" and an inline cache can remember that offset.
In dictionary mode, the object stores its properties in a hash table attached to the object itself. Each entry holds a key, a value, and attribute bits. The map no longer describes individual properties. It only says "this object uses a dictionary." Every object in dictionary mode can have its own unique set of keys without creating new maps.
| Aspect | Fast properties | Dictionary mode |
|---|---|---|
| Where the layout lives | Shared map with descriptor array | Per-object hash table |
| Property lookup | Fixed offset, cacheable | Hash and probe on each access |
| Inline cache benefit | Full: map check plus load at offset | Limited: the cache cannot store an offset |
| Cost of adding or removing keys | New map transition per shape change | Cheap insert or remove in the table |
| Memory per object | Compact, metadata shared | Larger, metadata stored per object |
| Good fit | Records and class instances with a fixed shape | Objects with many keys that change |
Dictionary mode is a deliberate trade-off. If every unusual shape change created a new map, V8 would fill memory with one-off maps and transition trees that never get reused. A hash table is the right structure for an object whose keys keep changing.
What Pushes an Object Into Dictionary Mode
V8 does not publish the exact rules, and they change between versions. The common triggers are stable enough to know.
Deleting a property that is not the last one added
Transition chains only move forward: an object gains a, then b. If you delete a, no map in the chain describes an object with "b but not a." V8 does not build a new branch for that case. It converts the object to dictionary mode.
// Run with: node --allow-natives-syntax delete-middle.js
const o = { a: 1, b: 2 };
console.log(%HasFastProperties(o)); // true
delete o.a;
console.log(%HasFastProperties(o)); // false: o is now in dictionary modeRecent V8 versions have a special case. If you delete the most recently added property, the engine can step back to the parent map in the transition chain, and the object stays fast.
// Run with: node --allow-natives-syntax delete-last.js
const o = { a: 1, b: 2 };
delete o.b; // b was the last property added
console.log(%HasFastProperties(o)); // true on recent V8 versionsAdding many properties dynamically
When code keeps adding new named properties to an object, especially with computed keys, V8 eventually stops extending the transition tree and switches the object to a dictionary. The threshold is an internal detail. It is small enough that an object used as a key-value store hits it quickly.
// Run with: node --allow-natives-syntax many-keys.js
const bag = {};
for (let i = 0; i < 1000; i++) {
bag['key' + i] = i; // string keys, not array indexes
}
console.log(%HasFastProperties(bag)); // falseThe example uses 'key' + i instead of plain numbers for a reason. Integer-like keys such as bag[5] go into the object's elements store, which is tracked separately from named properties. A later chapter on elements kinds covers that store.
Other triggers
Other operations can also cause the switch or keep an object slow. These include some uses of Object.defineProperty that change attributes on existing properties, and objects created with very large numbers of properties. Prototype objects get their own treatment. V8 tunes them for the fact that their methods are read often and changed rarely, and it may convert them between modes as they are set up. You rarely need to think about this case, but it explains why %HasFastProperties on a prototype can give surprising results.
Note that the diagram has no common arrow back from dictionary mode to fast mode. V8 can convert objects back in specific internal situations. Ordinary application code should assume the change is one-way.
What Dictionary Mode Costs
The main cost shows up in property access at hot call sites. An inline cache for a fast object stores "if the map is M, load from offset N." That is a pointer comparison and a memory load. For a dictionary-mode object, the map does not identify where the property is, because two objects with the same dictionary map can hold completely different keys. The access must hash the key and probe the table. V8 still caches what it can, but the work per access goes up, and the optimizing compiler has less to work with: it cannot turn the access into a direct field load.
A second cost hits other code. If dictionary-mode objects flow through a function together with fast objects, that function's call sites see more map variety. The next chapter on inline cache states explains why that makes call sites polymorphic or megamorphic.
Dictionary mode is not "slow" in absolute terms. A hash lookup takes nanoseconds. The cost matters only when an object is accessed millions of times on a hot path. For configuration objects, one-off results, or code that runs rarely, the difference cannot be measured.
Common Mistakes and Better Alternatives
Using `delete` to "clear" a field
A common habit is to delete a field once it is no longer needed. On a hot object, assign undefined or null instead. The shape stays the same and the object stays fast.
// Run with: node --allow-natives-syntax clear-field.js
class Session {
constructor(user, token) {
this.user = user;
this.token = token;
}
}
const s1 = new Session('ana', 'abc');
const s2 = new Session('ben', 'def');
delete s1.token; // changes shape: s1 goes to dictionary mode
s2.token = null; // keeps shape: s2 stays fast
console.log(%HasFastProperties(s1)); // false
console.log(%HasFastProperties(s2)); // trueThere is a semantic difference. After delete, 'token' in s1 is false. After assigning null, 'token' in s2 is still true. If other code checks whether the key exists, update those checks too.
Using a plain object as a hash map
When keys come from data, such as user IDs, words in a document, or cache entries, use Map. Map is built for frequent inserts and deletes. It does not touch hidden classes, and it accepts any key type.
const counts = new Map();
const words = 'the cat and the hat and the bat'.split(' ');
for (const w of words) {
counts.set(w, (counts.get(w) || 0) + 1);
}
counts.delete('cat'); // cheap and shape-neutral
console.log(counts.get('the')); // 3
console.log(counts.size); // 4An object that has gone into dictionary mode because it is used as a map is not a bug in itself. The engine is handling the pattern correctly. The real problem is passing such an object through code that also handles your structured, fast-shaped records.
Copying to recover fast mode
If you receive an object that went into dictionary mode and you will read it heavily afterward, you can build a new object with a fixed shape:
function normalize(raw) {
return { id: raw.id, name: raw.name, email: raw.email };
}The literal creates a fresh fast-mode object with a predictable map. Do this only when profiling shows that the reads are hot.
Exercise
An object is created as { a: 1, b: 2, c: 3 }. Which of these operations is most likely to put it into dictionary mode in V8, and why: o.c = undefined, delete o.c, or delete o.a?
Show answer
delete o.a. Assigning undefined leaves the shape unchanged. Deleting c, the last-added property, may let V8 step back to the parent map on recent versions. Deleting a removes a property from the middle of the transition chain, and no existing map describes the result, so V8 switches the object to dictionary mode.
Exercise
A function builds a lookup table with table[userId] = record for about 50,000 string IDs, and later removes entries with delete. Explain what happens to table internally and suggest a better structure.
Show answer
After enough dynamically added string keys, V8 converts table to dictionary mode, and the deletes keep it there. That is the correct representation for this usage, but a Map states the intent clearly, handles frequent inserts and deletes efficiently, and keeps hidden classes out of the picture. Replace it with const table = new Map(), table.set(userId, record), and table.delete(userId).
Exercise
Write a short script that shows that assigning null keeps an object fast while delete on a non-last property does not. Say which Node.js flag it needs.
Show answer
Run it with node --allow-natives-syntax. Example: const x = { p: 1, q: 2 }; const y = { p: 1, q: 2 }; x.p = null; delete y.p; console.log(%HasFastProperties(x), %HasFastProperties(y)); prints true false.