In programming, there are many ways to write lines of code to return the same result. Of these many ways, some are going to be faster than others. In general, fast is good!
The trick then is to both come up with faster solutions and be able to prove to yourself that a faster solution is indeed faster than something else you have tried. This article isn't going to help you with the first part, but it will certainly try to help you with the second part.
When measuring performance, it is always good to have things to compare against. For this article, let's say you have two ways of squaring a number - Math.pow and xn). What you want to do is figure out which of these approaches is faster:

Let's let the battle begin!
Meet the
Stopwatch
Before we pit our two contestants into a
bloody battle where only one will walk out alive, we need to
figure out what to use to measure performance. In real life,
to help
measure how fast something is, people tend to
use stopwatches. In ActionScript, your stopwatch equivalent
is the
getTimer function:
To be able to use this function, make sure you have the the import flash.utils.getTimer; statement added to the top of your code file.
What this function does is quite simple. The getTimer function returns the number of milliseconds that have elapsed since your application started running. Behind the scenes, the instant your application breathes life, an internal timer starts kicking. All getTimer does is return the value of that internal timer each time you call it.
By calling the getTimer function before and after some code, you can measure how many milliseconds it took to run that code by taking the difference of the two getTimer calls. The smaller the number, the faster your code!
Simple Example
Now that
you know what the getTimer function does, let's revisit the
initial problem by looking at a small example:
What I want to measure is the performance of calculating the Pythagorean theorem using the Math.pow function first. Because a single call in this case is too fast to measure, I am using a loop to artificially inflate the number of times that our one line gets called. Once you have run this code a few times, your elapsed time in milliseconds should start to fall within a very narrow range.
Next up, we want to change our theorem to use the x2 approach instead and compare the results. When comparing two particular approaches to see which one is faster, try to keep everything as same as possible with the exception of the variation you are measuring.
The x2 approach looks as follows:
Notice that almost everything is identical with the exception of the last line in the for loop where we replace Math.pow with x2.
At the end of both experiments, you can get an idea of which approach is faster. As you may have guessed from the very beginning, x2 is actually faster. Before going further and pursuing any action against Math.pow, read the following section first on how to increase the accuracy of your measurements.
Things to Keep in Mind
The example I used earlier is a quick and dirty way for
measuring the performance between two different approaches
that return the same result. If you are looking for more
accurate performance comparisons or wish to make sweeping
generalizations, try to keep the following list in mind.
There is a non-trivial amount of overhead to do the magic necessary to call a function, and that overhead will add milliseconds to your getTimer call. Inline what you are measuring instead.
Conclusion
Hopefully you
found this article on how to measure performance useful.
This article was inspired by
this thread, and the contents of the "Things to Keep in
Mind" section came largely from feedback by
Krilnon,
rumblesushi,
bwhiting, and
maskedman.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

:: Copyright KIRUPA 2026 //--