Macromedia Flex Macromedia Flex
Performance testing Equality Operations in AS3
  Home

Jul 13, 2006 - Performance testing Equality Operations in AS3
Null vs Null Object - How the new AVM Makes a difference

When evaluating for equality, is it better to use the null value directly or create a null object?

Werner Sharp from Adobe provides the following analysis:

var someInstance:Object = new Object();
var st = getTimer();
someInstance.foo = function() { }
for (var i:int = 0; i < 1000000; i++)
{
      someInstance.foo();
}
var et = getTimer();

Here are the numbers I get for three variants using the latest AVM+ build we have internally to use: 

  1. 167 msec – the loop above with no conditional (1 million iterations)
  2. 222 msec – a loop using “if someInstance != null”
  3. 182 msec – a loop using “if someInstance !== null” – strict equality operator
  4. 180 msec – using try/catch

In the specific case above, the reqular equality operator is the slowest.   I believe this is because someInstance is typed as an Object and not strongly typed as a specific class.  The E4X support requires strange equality tests – a text XML object with the text “null” is equal to the null value according to E4X.   So if you’re doing a comparison with a generic Object type and null, we need to do some XML checks on the types.   If someInstance was not a generic object, you wouldn’t see this slowdown.

E.g:

class MyClass
{
      function foo(){}
}
var someInstance:MyClass = new MyClass();

The times are quite different.   With a loop that has 10x the iterations (10 million), I get the following times…
113 – empty loop
117 - equality
122 – strict equality
250 – try/catch
(+-5 msec variations)

The overhead for equality drops way down once someInstance is a specific class.   Since the XML overhead checking is gone, the equality tests become nearly free.  You can also see the huge benefit for strong typing.   Since someInstance is known to be a MyClass object, calling the foo() function is very quick.

File Details
Created On Jul, 13, 2006 by Scott Russel
Last Modified On Jul, 13, 2006 by Scott Russel
Group: Tips and Articles
Flex Versions: 2.0
Category: Actionscript
Type: Tip
Difficulty: Intermediate
Keywords:
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80