Efficient looping through AS3 dictionary

2020-02-18 03:08发布

for (var k in dictionary) 
{
  var key:KeyType = KeyType(k);
  var value:ValType = ValType(dictionary[k]); // <-- lookup
  // do stuff
}

This is what I use to loop through the entries in a dictionary. As you can see in every iteration I perform a lookup in the dictionary. Is there a more efficient way of iterating the dictionary (while keeping access to the key)?

2条回答
够拽才男人
2楼-- · 2020-02-18 03:30

Iterate through keys & values:

for (var k:Object in dictionary) {
    var value:ValType = dictionary[k];
    var key:KeyType = k;
}

Iterate through values more concisely:

for each (var value:ValType in dictionary) {

}
查看更多
做个烂人
3楼-- · 2020-02-18 03:32

In AS3 there are 3 different for loops, you should use one that suits your needs best.

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth

查看更多
登录 后发表回答