A comment posted before the Flash Player 10.1 series of articles asked about the performance differences between creating an object with o = new Object() and with o = {}. Below I’ll look into the generated bytecode for these two approaches and test their relative performance to see if either approach is faster than the other.

First of all, let’s look at two extremely simple functions demonstrating the approaches:

public function curlyBraces(): void
{
	var o:Object = {};
}
 
public function newOperator(): void
{
	var o:Object = new Object();
}

Now let’s look at the bytecode generated by MXMLC 4:

  function curlyBraces():void	/* disp_id 0*/
  {
	// local_count=2 max_scope=1 max_stack=1 code_len=8
	0       getlocal0     	
	1       pushscope     	
	2       newobject     	{0}
	4       coerce        	Object
	6       setlocal1     	
	7       returnvoid    	
  }
 
 
  function newOperator():void	/* disp_id 0*/
  {
	// local_count=2 max_scope=1 max_stack=1 code_len=11
	0       getlocal0     	
	1       pushscope     	
	2       findpropstrict	Object
	4       constructprop 	Object (0)
	7       coerce        	Object
	9       setlocal1     	
	10      returnvoid  
  }

The disassembled versions of both of these functions are extremely simple and aren’t very different. Nevertheless, for AS3 code that does the exact same thing, they are different. The “curly braces” approach uses a special operator—newoperator—and then simply coerces it to an Object and sets it to the local variable o. On the other hand, the “new operator” approach uses findpropstrict to get the Object class, calls its constructor with constructprop, and then does a coerce and setlocal1 to set the local variable just like the “curly braces” approach did. So is it faster to use newobject or findpropstrict and constructprop? Let’s look at a simple performance test to find out:

package
{
	import flash.display.*;
	import flash.text.*;
	import flash.utils.*;
 
	/**
	*   An app to test the speed of different ways to create an object
	*   @author Jackson Dunstan (jacksondunstan.com)
	*/
	public class ObjectCreation extends Sprite
	{
		public function ObjectCreation()
		{
			var logger:TextField = new TextField();
			logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(logger);
			function log(msg:*): void { logger.appendText(msg+"\n"); }
 
			var i:int;
			var beforeTime:int;
			var afterTime:int;
			var o:Object;
			const REPS:int = 10000000;
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				o = {};
			}
			afterTime = getTimer();
			log("Curly braces: " + (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				o = new Object();
			}
			afterTime = getTimer();
			log("New operator: " + (afterTime-beforeTime));
		}
	}
}

Here we are simply using the two approaches a lot of times and measuring how long they take. Let’s look at the results:

Environment Curly Braces New Operator
3.0 Ghz Intel Core 2 Duo, Windows XP 1842 1254
2.0 Ghz Intel Core 2 Duo, Mac OS X 3218 2341

In a surprise (to me, at least) result, the “curly braces” approach with its one special-purpose instruction (newobject) is definitively beaten by the “new operator” approach with its two general-purpose instructions (findpropstrict and constructprop). The “new operator” approach claims a 1.47x speedup on Windows XP and a 1.37x speedup on Mac OS X. So if you’re looking to be performance-conscious and don’t mind a bit of extra typing, by all means prefer o = new Object() over o = {} when constructing empty objects. Also, if you have any AS3 questions like the comment this article is in response to, by all means feel free to ask in the comments section below.