4 ms·
I'm saying that argument is wrong, because there is no efficiency loss due to different insertion orders mapping to the same hidden class. In fact there are eff
by gsg 12y ago
I'm saying that argument is wrong, because there is no efficiency loss due to different insertion orders mapping to the same hidden class. In fact there are efficiency advantages because more objects would pass through class-based guard checks into favourable paths. The only reason I can see to not do that is because it would lose iteration order.
Sketch out an example of hidden class transitions and it should be clear:
In the order preserved case (what JS engines actually do):
start with {} of hidden class <empty>
add .x, transition to hidden class <A> with field .x at 0
add .y, transition to hidden class <B> with field .x at 0 and field .y at 1
start with {} of hidden class <empty>
add .y, transition to hidden class <C> with field .y at 0
add .x, transition to hidden class <D> with field .y at 0 and field .x at 1
In the order discarded case:
start with {} of hidden class <empty>
add .x, transition to hidden class <A> with field .x at 0
add .y, transition to hidden class <B> with field .x at 0 and field .y at 1
start with {} of hidden class <empty>
add .y, transition to hidden class <C> with field .y at 0
add .x, transition to hidden class <B> with field .x at 0 and field .y at 1
In the second case the field .y changes offset, but that's fine because the hidden class also changes to indicate the difference in structure. The only problem is that the order of the fields changes.
- Arnavion 12y agoBy the way, JS runtimes used to have insertion order for enumerating properties long before the hidden class optimization or V8 was a thing. V8 was the first to break the insertion-order enumeration for some kinds of objects [1] and eventually other browsers followed suit, even those that don't do hidden classes. [1] http://code.google.com/p/v8/issues/detail?id=164 http://code.google.com/p/v8/issues/detail?id=164
- gsnedders 12y agoWho doesn't do "hidden classes" (or maps, or inferred-classes, or whatever else we're calling them today)? All major JS engines certainly do.
- Arnavion 12y agoMy point was that hidden classes is not a reason to keep nor break insertion-order enumeration. The example I gave was V8 breaking insertion-order enumeration, but for a reason entirely unrelated to its use of hidden classes - it implements Arrays as Objects with consecutive numeric keys, so Objects with consecutive numeric keys get enumerated like Arrays. So yes, while all major browsers use hidden classes today, that itself is not sufficient to explain why insertion-order enumeration is not maintained.