• Advertisement
Sign in to follow this  

[web] [AS3] Hash Table or Named Property

This topic is 2968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I'm working on an AS3 game engine. I have several objects which have names, and are stored in an internal class which combines the name, objects, and any related data, then stores each of these in a Vector. Here is a quick example:
// Test.as
public class Test {
    protected var namedData:Vector.<NamedData> = new Vector.<NamedData>();

    public function Test() {}

    public function Add(name:String, data:Object):void {
        namedData.push(new NamedData(name, data));
    }
}

internal class NamedData {
    protected var name:String;
    protected var data:Object;

    public function NamedData(name:String, data:Object) {
        this.name = name;
        this.data = data;
    }
}
with relevant accessors, get functions, etc. Now, the reason for this post: For get function I loop through each of the data, looking for a match to the name. When I find it, I return it. (There are proper things in place to prevent duplicate names getting added and what-not). My question is: Do you think a Hash Table would give me a significant enough performance boost that I should reconsider how I do these types of classes, so I could go from something like:
function Get(name:String):Object {
    for each(var d:NamedData in namedData)
        if(d.Name == name)
            return d.Data;
}
to this:
function Get(name:String):Object {
    if(hash[name])
         return hash[name];
}
??? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hashtables - yes, they'd certainly boost up the speed of execution, but it'd be pretty much unnoticable. It depends on how many objects you're iterating through, and how frequently you need to get them. If its large, you'd better use hashtables.

Also, the code should look like this:

function Get(name:String):Object {
if(hash[name])
return hash[name];
else return null;
}

Or even better, like this:
function Get(name:String):Object {
if(hash[name])
return hash[name];
else throw new ReferenceError("Property [" + name + "] not found.");
}

Share this post


Link to post
Share on other sites
Yeah, I know, I was just typing out a quick example. Thanks though. =p

For most of them, the number is likely to be relatively few (100 would be an extreme max), but others could potentially have several thousand.

Right now I'm only retrieving the data when it changes and keeping track of just what I need, so it isn't a big hit.

But, since I'm planning on releasing this to the public, and it's somewhat targeted at novices, I think it's best to get whatever potential performance boosts I can for them, since they won't always have the experience to do so themselves.

Is using an Object the best way to do hash tables, or should I -fake- a hash-table like scenario using something like Vector?

Thanks.

Share this post


Link to post
Share on other sites
Well, I decided not to use hash tables, mainly because there isn't any flexible way I can implement them and still keep everything strongly-typed.

I implemented a binary search for accessing them, so the speed should be roughly the same, given the relatively small number of elements that will be stored.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement