Saturday, June 23, 2007

Behave yourself, substring.

str.substring(start, end)

According to the AS documentation, if end is -1, it's assumed to be 0. So what does it mean when you have "abcdefg".substring(2, 0)? It means that it will returning the substring starting at position and going backward excluding the last one. So you get "ab" in this case.

What happen if you have str.substring(2, undefined) or str.substring(2, undefined - 1); ? Surprisingly, the behavior is different in either case.
undefined - 1 will give you an NaN. In the first case, passing undefined behaves the same as not passing anything for end. Passing NaN however, makes it behave as if you are passing in a negative number or 0.

Why should I know all this, you ask? Well, if you are using substring in a loop, with the end being a variable, then you must be aware of what will happen if that variable becomes undefined. Of course, the best thing in most cases is to guard your code against that.

Tuesday, June 19, 2007

I am working on a project that uses AMFPHP right now. I was streaming in a fair amount of data, and things were performing quite well while running it locally. When I finally looked at it online, the app was chugging (from 30fps down to 12 fps) while it's being side-scrolled. After some investigations, I found out it was because I was instantiating 2000+ objects that contains the deserialized data. As soon as I limit the objects down to 500 and keep the rest of the data in one string in CSV format, the app's performance went back up again.

The moral of the story: Watch out not only on the number of movieclips you have on stage at any given time, but also how many objects you are creating.

Right now the load time for the 2000+ datapoints (each taking ~25 bytes, not counting the extra bytes AMF uses while encoding it) is also taking quite a while -- considering that the data size is less than 100KB. I am going to try using CSV string instead of the native data types in the AMFPHP end, gzip it and see how that goes. Will follow up with a report on that end soon.