2002 posts
  • Has referred 50+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
bitfade says

I’m posting this coz i’ve read a lot of threads about flash code optimizations like bitwise math, fast alternatives to slow Math.* functions, callbacks vs events and so on.

well, here’s the truth about this kind of optimizations: 99.99% they are just useless.

AS3 (AVM2) is fast, way faster then you could ever need in most real world applications.

plus, flex/flash compilers su**s…...

they will never optimize your code, basicly this means that you could write something simple like a+b+c in thousands of different ways and the compiler will always produce a different bytecode.

so, when you try to optimize math, you’ll end up writing the same thing again and again searching for which one produces a better (faster) compiler code.

but, again, this is useless for almost any item here on AD

my current work involve moving 2500 3D particles at 25fps, this means something like 20-30 float multiplications for each particle/iteration.

well, if total project cpu usage is 100, all this math stuff only requires 5-6.

First thing you’ll need to optimize for getting better performances is the display list: reduce number of objects, simplify complex vector mcs (or convert them to bitmapdata) and most important: only use scaling and filter when really necessary.

a single filter can eat like 20/25% of your total project cpu requirements.

Also, reuse object: do not create Points, Rectangles or other simple objects each time you need them.

Each time you use the “new” operator, a new istance has to be destroyed at some point: the Garbage Collector will take care but this doesn’t comes free.

Another thing you should know is that memory access is way slower than math.
Array are slow, untyped objects slower.

Things are better now with Vectors but still if you do a lot of read/write operations, you may get faster code by reducing them.

1955 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
Emroni says

What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?

Understand the reusing of objects part, thats a really good one, especially when working with 3D objects.

Agree with the Math not being such a big problem. I mean, if its 1 ms faster one way than the other, it is nothing compared to using a filter on an object that consumes percentages of cpu.

4140 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $5,000+ on Envato Market
  • Located in Brazil
+3 more
tsafi says

Well I don’t know about converting complex vector to bitmapdata as long as your bitmap is static as a holder but it take a lot if you don’t use it in the right way and it’s a tricky subject . Graphic/shape class are lighter but they don’t carry the Sprite syntax function .

I agree filters are big enemy when it comes to Cpu. And a reuse objects is great with class less with time line base

Let me add any kind of for loop the less you use the faster things will move for you. Removing for loop is a pain subject on large projects like templates.

Also Tween engine….. well the list goes on … :p

And why use the useless word, its not :P

2002 posts
  • Has referred 50+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
bitfade says
What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?
according to flex profiler: if you call BitmapData.dispose() and then set to undefined all references, memory is freed almost instantly

2671 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 100+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
Chuckanucka says

great post! thanks.

2002 posts
  • Has referred 50+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
bitfade says
And why use the useless word, its not :P
well, i didn’t said they are 100% useless :P
4140 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $5,000+ on Envato Market
  • Located in Brazil
+3 more
tsafi says
And why use the useless word, its not :P
well, i didn’t said they are 100% useless :P

Ti perdono :P

5360 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says

Great tips :) Why is scaling bad?

1955 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
Emroni says
according to flex profiler: if you call BitmapData.dispose() and then set to undefined all references, memory is freed almost instantly

Got it. Then I have the following scenario:

- Load image with Loader class
- Create ‘bmp’ variable, a Bitmap
- Place image in bmp with ‘bmp = Bitmap(loader.content)’

Now we have both the image in the loader as well as the image in the bmp variable. Can we delete the image in the loader without losing the image in the bmp? And how?

I always use ‘loader = null’, but dont have the idea that that works. I also heard that dispose() wont work if there is still a reference (the bmp in this case) to that image. Thoughts?

5360 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says
What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?

If the life-cycle of your object has ended, destroying it will free memory and improve performance. But only when the GC decides to run a sweep. Not sure about BitmapData. I always thought thesame principle applied, but if it frees memory immediately that’s awesome.

by
by
by
by
by
by