I was sitting here thinking about a couple aspects of the symbol/weakmap future and I realize that I want to lay out some of the incentives I see being created by these changes.
First things first: according to recent benchmarks, symbol comparison is still twice as fast as string comparison in a microbenchmarks. I have no reason to think this is an abberration, as it fits with my hypotheses about the fundamental limitations of JS perf. To be clear, the comparisons should always be between interned strings so falling back to character-by-character comparison should never be necessary, but this does not invalidate the result.
That strongly suggests to me that in the future the fastest code for handling trees, especially syntax trees, will look like a bit like this:
let types = {
IfExpression: Symbol('IfExpression')
}
let node = {
type: types.IfExpression,
value,
}
What's interesting about that it is in some sense fundamentally equivalent to this:
class Node {
value;
}
That's because the presence of a symbol-type type
, when conjoined with the presence of weak maps, turns a symbol typed object into something which can have one (or arbitrarily many) prototypes associated with it like this:
let prototypes = new WeakMap();
// set up some prototypes
prototypes.get(node.type);
I think this is in ways the most spectacular single development in the JS language, ever. Just by existing this change completely unifies the object and class structures, and introduces new class-like patterning under which multiple inheritance and the diamond dependency problem are solved for.
Here's what I want to ask about though: if you don't wish to construct a de-facto class, you have two basic choices. Either you always create a new symbol every time (likely useless as it will then never ===
anything) or you can register the symbol somewhere in the global string namespace.
So I'm wondering: does the committee have any thoughts about the perverse incentives that might be created to register symbols in the global namespace merely to avoid turning symbol-typed plain objects into classes when that behavior is not desirable?