Marty
Marty

Reputation: 39456

How can I achieve fast and effective String compression in Actionscript 3?

I have an Object which stores pairs for a find and replace that I perform on up to 1500 Strings at a time.

The Object is populated with pairs using a method that will accept a String and then store this as a property with the value being an automatically assigned base 36 number, like this:

function addShort(long:String):void
{
    _pairs[long] = _nextShort;
}

_nextShort returns an automatically incremented value being the subject of .toString(36), so running the above a few times might make _pairs look like this:

_pairs:Object = {
    "class": "0",
    "testing.objects.TestBlock": "1",
    "skin.x": "2",
    "skin.y": "3",
    ...........
    "someString": "az1"
};

This Object could realistically end up being really large, having over a couple hundred pairs stored.

I then have a method that will take a "long" String (which will include the Strings I've given to addShort() previously) and return a new String where these have been replaced with their respective short value.

The method looks like this:

public function shorten(long:String):String
{
    for(var i:String in _pairs)
        long = long.split(i).join(_pairs[i]);

    return long;
}

Nice an simple, however in my mind I foresee a massive problem in a case where I might want to "shorten" 2000+ Strings and the _pairs Object has at the same time has over 500 pairs.

That ends up being 1,000,000 iterations all up which obviously doesn't seem very efficient at all.

How can I improve this process significantly?

Upvotes: 1

Views: 585

Answers (2)

Marty
Marty

Reputation: 39456

Based on comments from @kapep I realized what I needed is actually a compression library that will do this work for me.

I stumbled across an LZW compression class within a package called Calista which works great.

I did notice that the compression was really slow, which is understandable, but if there are any suggestions for something quicker I'm open to them.

Upvotes: 1

ncreated
ncreated

Reputation: 641

How about Regular Expressions for replacing String patterns? Catch some code.

Upvotes: 0

Related Questions