Type Casting
From ActionScript Performance Wiki
Contents |
Purpose
Compare which method of type casting is faster for manipulation a value object.
Code
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.utils.setTimeout; public class Test extends Sprite { public var textField:TextField = new TextField(); public var a:Array = []; private const STRING:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private const NUMBER:Number = 1234567890; public function Test() { textField.autoSize = TextFieldAutoSize.LEFT; addChild(textField); setupTestVariables(); setTimeout(runtest, 2000); } private function setupTestVariables():void { var vo:ValueObject = new ValueObject(); a[0] = vo; } private function runtest():void { textField.text = new PerformanceComparison(1000000, test1, test2, test3).start(); } private function test1():void { ValueObject(a[0]).string = STRING; ValueObject(a[0]).number = NUMBER; } private function test2():void { (a[0] as ValueObject).string = STRING; (a[0] as ValueObject).number = NUMBER; } private function test3():void { a[0].string = STRING; a[0].number = NUMBER; } } } internal class ValueObject { public var string:String; public var number:Number; }
Results
Compiler version: 3.3 , Player version: MAC 10.0.22.87, Operating System: Mac OS 10.5.8
- Test 1
- 280 ms, 19% slower
- Test 2
- 235 ms, best
- Test 3
- 462 ms, 97% slower
Compiler version: 3.3.04852, Player version: WIN 10.0.45.2, Operating System: Windows XP
- Test 1
- 462 ms, 17% slower
- Test 2
- 396 ms, best
- Test 3
- 616 ms, 56% slower
Conclusion
Casting the type directly (eg. Type(object)) performs better for accessing a value object. This solution may not work as expected with the types Array and Date.

