To "This" or not to "This"
That was always my question when creating my applications and I never found definitive answers online about whether using “this” or not using “this” was better for my applications.
Yes I enjoyed the “filling in” that Eclipse/Flex Builder would do for me, but I always felt guilty wondering if this practice was bad for my applications. My curiosity went a bit further, I wondered if using static constants within the same object was good or bad for my application. So I went forward and tested it.
Here is the code that I used for my test cases. The main application:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="onCreationComplete(event)">
<mx:Script>
<![CDATA[
import mx.managers.SystemManager;
import com.unitedmindset.vo.PersonVo;
import mx.events.FlexEvent;
[Bindable]
private var avgTimeTook:Number;
[Bindable]
private var avgMemoryChange:Number;
private function onCreationComplete(event:FlexEvent):void
{
//set time took
avgTimeTook = new Number();;
}
private function run(event:MouseEvent):void
{
//total time
var totalTime:Number = new Number();
//total memory change
var totalMemoryChange:Number = new Number();
for(var j:int=0;j<number(avgNumber.text);j++){
//var list
var a:Array = new Array();
//time took
var timeTook:Number = new Number();
// memory
var memory:Number = System.totalMemory;
//start time
var startTime:Date = new Date();
//create vos
for(var i:int=0;i<number(voCount.text);i++)
a.push(new PersonVo());
//end time
var endTime:Date = new Date();
//set time took
totalTime += endTime.time - startTime.time;
//set memory
totalMemoryChange += System.totalMemory - memory;
}
avgMemoryChange = totalMemoryChange / Number(avgNumber.text);
avgTimeTook = totalTime / Number(avgNumber.text);
}
]]>
</mx:Script>
<mx:Text text="Number to Avg"/>
<mx:TextInput text="1000" id="avgNumber"/>
<mx:Text text="Number of VOs to Create"/>
<mx:TextInput text="10000" id="voCount"/>
<mx:Button label="Run" click="run(event)"/>
<mx:Text text="Avg Time Took: {avgTimeTook} ms"/>
<mx:Text text="Avg Memory Consumed: {avgMemoryChange} bytes"/>
</mx:Application>
And the “PersonVo” that I created for my tests.
{
public class PersonVo
{
public static const FIRST_NAME:String = "Joe";
public static const LAST_NAME:String = "Blow";
public static const EMAIL:String = "joeblow@unitedmindset.com";
public static const PHONE_NUMBER:String = "999.999.9999";
public static const COUNTRY:String = "USA";
public static const ADDRESS:String = "my place";
public function PersonVo()
{
this.firstName = PersonVo.FIRST_NAME;
this.lastName = PersonVo.LAST_NAME;
this.email = PersonVo.EMAIL;
this.phoneNumber = PersonVo.PHONE_NUMBER;
this.country = PersonVo.COUNTRY;
this.address = PersonVo.ADDRESS;
}
public var firstName:String;
public var lastName:String;
public var email:String;
public var phoneNumber:String;
public var country:String;
public var address:String;
}
}
For my tests I adjusted the constructor of the “PersonVo”.
Test 1: With “this” / No reference
For the first test my constructor looked as such:
{
this.firstName = "Joe";
this.lastName = "Blow";
this.email = "joeblow@unitedmindset.com";
this.phoneNumber = "999.999.9999";
this.country = "USA";
this.address = "my place";
}
The results were:
Avg Time Took: 19.034 ms
Avg Memory Consumed: 24649.728 bytes
Test 2: Without “this” / No reference
For the second test my constructor looked as such:
{
firstName = "Joe";
lastName = "Blow";
email = "joeblow@unitedmindset.com";
phoneNumber = "999.999.9999";
country = "USA";
address = "my place";
}
The results were:
Avg Time Took: 16.137 ms
Avg Memory Consumed: 40828.928 bytes
Test 3: Without “this” / Long reference
For the third test my constructor looked as such:
{
firstName = PersonVo.FIRST_NAME;
lastName = PersonVo.LAST_NAME;
email = PersonVo.EMAIL;
phoneNumber = PersonVo.PHONE_NUMBER;
country = PersonVo.COUNTRY;
address = PersonVo.ADDRESS;
}
The results were:
Avg Time Took: 16.345 ms
Avg Memory Consumed: 45711.36 bytes
Test 4: Without “this” / Short reference
For the fourth test my constructor looked as such:
{
firstName = FIRST_NAME;
lastName = LAST_NAME;
email = EMAIL;
phoneNumber = PHONE_NUMBER;
country = COUNTRY;
address = ADDRESS;
}
The results were:
Avg Time Took: 17.373 ms
Avg Memory Consumed: 23097.344 bytes
Test 5: With “this” / Long reference
For the fifth test my constructor looked as such:
{
this.firstName = PersonVo.FIRST_NAME;
this.lastName = PersonVo.LAST_NAME;
this.email = PersonVo.EMAIL;
this.phoneNumber = PersonVo.PHONE_NUMBER;
this.country = PersonVo.COUNTRY;
this.address = PersonVo.ADDRESS;
}
The results were:
Avg Time Took: 19.388 ms
Avg Memory Consumed: 31637.504 bytes
Test 6: With “this” / Short reference
For the sixth test my constructor looked as such:
{
this.firstName = FIRST_NAME;
this.lastName = LAST_NAME;
this.email = EMAIL;
this.phoneNumber = PHONE_NUMBER;
this.country = COUNTRY;
this.address = ADDRESS;
}
The results were:
Avg Time Took: 16.646 ms
Avg Memory Consumed: 5873.644 bytes
For each of the tests I created 10,000 PersonVos and ran the test an average of 1000 times to get quantifiable numbers. Feel free to run the numbers yourself and report your own results. Based on these tests, the best combination that I seemed to have found is using “this” and the short reference when possible.




