Sub topic 13
Where Proxies Fall Short: Internal Slots, Private Fields, Identity, and Performance
This chapter assumes you already know how to write get traps, forward with Reflect.get, and use the receiver argument. Proxies intercept every operation that goes through an object's internal methods. Some JavaScript features bypass those methods entirely, and in those cases a proxy cannot help or gets in the way.
Internal Slots in Built-ins
Many built-in objects keep their data in internal slots. An internal slot is hidden storage that the engine attaches to an object when it creates it. A Map keeps its entries in [[MapData]], a Date keeps its time in [[DateValue]], and a Set keeps its values in [[SetData]]. Methods such as Map.prototype.set look for that slot on this.
When you call a method through a proxy, this is the proxy. The proxy has no [[MapData]] slot, so the method throws.
const map = new Map();
const broken = new Proxy(map, {});
try {
broken.set('a', 1);
} catch (err) {
console.log(err instanceof TypeError); // true: incompatible receiver
}
const working = new Proxy(map, {
get(target, prop) {
const value = Reflect.get(target, prop, target);
return typeof value === 'function' ? value.bind(target) : value;
}
});
working.set('a', 1);
console.log(working.get('a')); // 1
console.log(working.size); // 1The fix has two parts. Methods are bound to the target, so this holds the real slot. The size getter is read with target as the receiver, because size also checks for [[MapData]].
Arrays do not have this problem. Array data lives in ordinary properties, so new Proxy([], {}) works normally, and Array.isArray sees through the proxy.
Private Class Fields
Private fields such as #count behave like internal slots. The engine looks them up directly on the object and never calls a trap. A proxy object has no private fields of its own, so a method that reads this.#count fails when this is the proxy.
class Counter {
#count = 0;
increment() {
return ++this.#count;
}
}
const plain = new Proxy(new Counter(), {});
try {
plain.increment();
} catch (err) {
console.log(err instanceof TypeError); // true
}
const bound = new Proxy(new Counter(), {
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
return typeof value === 'function' ? value.bind(target) : value;
}
});
console.log(bound.increment()); // 1The same binding workaround applies here, and it has the same cost. Once a method runs with this set to the target, any property access inside it skips your traps. Each read of the method also returns a new bound function, so bound.increment === bound.increment is false. If you need the function to stay the same between reads, cache the bound functions in a WeakMap keyed by the original function.
Identity
A proxy is a separate object from its target. Code that compares objects by identity treats them as different.
const target = {};
const proxy = new Proxy(target, {});
console.log(proxy === target); // false
console.log(new Set([target]).has(proxy)); // false
const meta = new WeakMap([[target, 'info']]);
console.log(meta.get(proxy)); // undefinedThis causes bugs when a library stores objects in a WeakMap or Set and later receives the proxy instead. No trap can change this, because === and map lookups do not call internal methods. Decide which reference is the public one and hand out only that reference.
Performance
Every trapped operation costs an extra function call, and the Reflect call inside it adds more. Engines also optimize property access on ordinary objects with techniques such as inline caches, and those optimizations do not work as well through a proxy. A proxy in a tight loop or on a hot data path can be noticeably slower than the plain object.
| Limitation | Cause | Workaround |
|---|---|---|
| Built-in methods throw | Internal slots missing on proxy | Bind methods to target; read getters with target as receiver |
| Private fields throw | `#field` lookup skips traps | Bind methods to target |
| Identity mismatch | Proxy is a distinct object | Expose only one reference |
| Slower access | Trap call on every operation | Keep proxies off hot paths; measure |
Exercise
You wrap a Date in new Proxy(new Date(), {}) and call getTime() on the proxy. What happens, and how do you fix it?
Show answer
It throws a TypeError, because getTime needs the [[DateValue]] internal slot and this is the proxy, which lacks it. Add a get trap that binds function values to the target, for example typeof v === 'function' ? v.bind(target) : v.
Exercise
A library caches results in a WeakMap keyed by the original object. After you pass it a proxy of that object, the cache always misses. Why can't a trap fix this?
Show answer
WeakMap lookup compares object identity directly and never calls a proxy trap. The proxy is a different object from the target, so the key never matches. Pass the library one reference consistently, either always the proxy or always the target.