Operation sequence designation
I wrote the following code
o[s += "a"] = o[s += "b"] = o[s += "c"] = 0;
I was curious why the variable s
ends up storing "abc" and not "cba". It looks like the code will be easier to run from right to left.
It seems like the performer is picking and indexing for storage and then deciding what's going on there, but that makes the sound slower as there will be so many states and memory on the stack. Can someone explain why it makes sense to execute the code in the order that it is?
I have included a fiddle with some more examples. http://jsfiddle.net/eggrdtuk/3/
source to share
Luke's comment is right. We can look at the MDN page for operator priority.
Computed member access has priority 1 and assignment priority 16. Thus, the property access expressions are executed first, and then the assignment operations. In addition, Computed Member Access has left-to-right associativity, while Assignment has right-to-left associativity.
So, we can think of the first step as parsing property access expressions from left to right:
o["a"] = o["ab"] = o["abc"] = 0
and the second step is with the right to left settings:
(o["a"] = (o["ab"] = (o["abc"] = 0)))
(o["a"] = (o["ab"] = 0))
(o["a"] = 0)
I don't see how changing the associativity for any step would change performance. But if there is a reason, I would love to know :)
source to share