The right use for this feature is to turn any Object into an associative array. ActionScript doesn't have an associative array class but it doesn't need one, as any Object, provided it's class is dynamic, can have members added and then referenced by name, as if it were an associative array. There are many uses for this, although not many in a web game.
But the class only needs to be dynamic to add members at runtime. Members that are part of the class definition can be read by name. The syntax looks like this:
iSum += v["iX"] + v["iY"];
Compared with accessing the members directly.
The problem with doing this is it's slow, incredibly so. I've updated my accessor test (embedded below) to include the above, and it is ten times slower than using accessor functions, fully fifty times slower than accessing the members directly.
And that's not the worst example I've seen. The worst looks something like this.
level = game["level" + level_num];
preceding the lookup with a relatively expensive String addition. This could be replaced with an array lookup. If a String is used as it contains more information then replace them with constants. E.g.
level = game.levels[kLevelBase + level_num];
Unless a String won't be known until runtime it is always possible to replace it with constants as above.
No comments:
Post a Comment