1

Suppose we have some custom object type:

class SomeObjectType {
   public var intProperty1:int;
   public var intProperty2:int;
   public var intProperty3:int;
   public var stringProperty1:String;
   public var stringProperty2:String;
   public var stringProperty3:String;
   public var stringPropertyThatActuallyIsInt1:String;
   public var stringPropertyThatActuallyIsInt2:String;
   public var stringPropertyThatActuallyIsInt3:String;
   ...
   %ABOUT_20_ANOTHER_PROPERTIES_THAT_I_WON'T_USE%
}

We have a collection of more than 20k instances of these objects. And we have just 1 text input that is actually search filter. User can type in this filter field anything he want and if his filter matches with ANY of first 9 fields I described before we should leave this object in collection. Just simple items filtering.

And let me describe how it works in our project now. This algorithm casts all these properties to Strings, concatenate them, and search using indexOf() != -1 method. This is really slow. It takes about 500-900ms on my dev machine and about 3-4s on iPad on every filter change. Horrible statistics, isn't it?

Small note: we use this algorithm in different 3 places in app and objects differs from object I described above but idea is same. I believe that it is a good idea to compare int to int, string to string (implementing some of fast algorithms(there are lots if them)), and convert string that is actually to int and compare them as int to int, but object differs a lot so I need some common algorithm.

4

2 回答 2

2

如果集合是指 ArrayCollection,我建议改用Vector

向量比 ArrayCollections 快大约 50 倍。

如果你需要数据绑定,你可以看看VectorCollection,但我无法想象性能会接近 Vector。

此外,如果您没有在任何地方扩展类 SomeObjectType,则可以通过将其设为最终类 SomeObjectType 来获得一些性能(尤其是在 iOS 上)。

于 2013-06-21T20:49:36.730 回答
0

你可以用字典搜索,我觉得会快很多,你只需要初始化一次。

var dict:Dictionary = new Dictionary();

//get properties,in you obj, they are intProperty1,intProperty2 etc,exclude prototype
var properties:Array = ObjectUtil.getClassInfo(SomeObjectType, ["prototype"]).properties as Array;

for each (var obj:SomeObjectType  in yourArrayCollection) {

   for (var i:int = 0; i < properties.length; i++) {

        var qname:Object = properties[i];
        var k:String = qname.localName;

        var v:String = obj[k];

        if (dict[v] == null) {
            dict[v] = [];
        }

        //add only one time
        if ((dict[v] as Array).indexOf(obj) == -1) {
            (dict[v] as Array).push(obj);
        }
   }

}

所以如果你想返回所有包含“5”的objs,只需返回dict[5]

于 2013-06-22T01:46:03.430 回答