在 as3 中获取对象值的最快方法

发布于 2024-09-15 10:07:04 字数 498 浏览 9 评论 0原文

好吧,我发誓这个问题应该无处不在,但事实并非如此。

我有一个值对象,里面有很多 getter/setter。它不是一个动态类。我迫切需要搜索充满它们的 ArrayCollection。搜索涵盖所有领域,因此我将使用大约 13 种不同类型的 VO。

我尝试过 ObjectUtil.toString() ,效果很好,但速度慢得要命。有 20 个属性需要返回,并且 ObjectUtil.toString() 在输出中添加了一堆垃圾,更不用说代码一开始就很慢。

flash.utils.describeType() 更糟糕。

我会很高兴听到我遗漏了一些明显的东西。

更新: 我最终采用了 Juan 的代码以及用于搜索的过滤算法并创建了 ArrayCollectionX。这意味着我现在使用的每个 ArrayCollection 都处理它自己的过滤器。我可以搜索 AC 中项目的各个属性,或者使用 Juan 的代码,它可以像冠军一样处理完整的集合搜索。与使用外部滤波器的相同解决方案相比,延迟可以忽略不计。

Ok, so I swear this question should be all over the place, but its not.

I have a value object, inside are lots of getters/setters. It is not a dynamic class. And I desperately need to search an ArrayCollection filled with them. The search spans all fields, so and there are about 13 different types of VOs I'll be doing this with.

I've tried ObjectUtil.toString() and that works fine and all but it's slow as hell. There are 20 properties to return and ObjectUtil.toString() adds a bunch of junk to the output, not to mention the code is slow to begin with.

flash.utils.describeType() is even worse.

I'll be pleased to hear I'm missing something obvious.

UPDATE:
I ended up taking Juan's code along with the filter algorithm I use for searching and created ArrayCollectionX. Which means that every ArrayCollection I use now handles it's own filters. I can search through individual properties of the items in the AC, or with Juan's code it handles full collection search like a champ. There was negligible lag compared to the same solution with external filters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-09-22 10:07:04

如果我正确理解你的问题,你想要的是为某些对象定义的吸气剂列表。据我所知,您必须使用describeType来完成类似的事情(我很确定ObjectUtils在幕后使用了这个方法)。

正如您所注意到的,大量调用describeType会很慢。但对于只有 13 种类型,我认为这应该不成问题。由于这些类型不是动态的,因此您知道它们的属性是固定的,因此您可以检索此数据一次并将其缓存。您可以预先构建缓存,也可以在发现新类型时构建缓存。

这是在代码中执行此操作的简单方法:

private var typePropertiesCache:Object = {};

private function getPropertyNames(instance:Object):Array {
    var className:String = getQualifiedClassName(instance);
    if(typePropertiesCache[className]) {
        return typePropertiesCache[className];
    }
    var typeDef:XML = describeType(instance);
    var props:Array = [];
    for each(var prop:XML in typeDef.accessor.(@access == "readwrite" || @access == "readonly")) {
        props.push(prop.@name);
    }   
    return typePropertiesCache[className] = props;
}

If I understand your problem correctly, what you want is a list of the getters defined for certain objects. As far as I know, you'll have to use describeType for something like this (I'm pretty sure ObjectUtils uses this method under the hood).

Calling describeType a lot is going to be slow, as you note. But for only 13 types, this shouldn't be problematic, I think. Since these types are not dynamic, you know their properties are fixed, so you can retrieve this data once and cache it. You can build your cache up front or as you find new types.

Here's is a simple way to do this in code:

private var typePropertiesCache:Object = {};

private function getPropertyNames(instance:Object):Array {
    var className:String = getQualifiedClassName(instance);
    if(typePropertiesCache[className]) {
        return typePropertiesCache[className];
    }
    var typeDef:XML = describeType(instance);
    var props:Array = [];
    for each(var prop:XML in typeDef.accessor.(@access == "readwrite" || @access == "readonly")) {
        props.push(prop.@name);
    }   
    return typePropertiesCache[className] = props;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文