Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it faster to set a reference to a local variable of a nested object in ActionScript or the same?
    primarykey
    data
    text
    <p>I have an object that is nested several levels deep which I'm referencing multiple times. Is it faster to create and set a reference to that object or does it matter? </p> <p>Context I heard that it is faster to create a local reference. This is years ago and for a Visual Basic project. But this is Flash. And the output is a SWF / bytecode. And the compiler could look at that reference and do what I'm doing so that the object does not have to be looked up each time. </p> <p>For example:</p> <pre><code>public function doStuff():void { Model.instance.view1.button1 = button1; Model.instance.view1.button2 = button2; Model.instance.view1.button3 = button3; Model.instance.view1.button4 = button4; Model.instance.view1.button5 = button5; Model.instance.view1.button6 = button6; // more fake code referencing more something.something.something like things for (var i:int;i&lt;something.something.something.length;i++) { var result:Object = Controller.staticMethod(button1); var result2:Object = Controller.staticMethod(button1); } } </code></pre> <p>Vs:</p> <pre><code>public function doStuff():void { var view1:View = Model.instance.view1; view1.button1 = button1; view1.button2 = button2; view1.button3 = button3; view1.button4 = button4; view1.button5 = button5; view1.button6 = button6; } </code></pre> <p>.<br> Taking Baris suggestion I tested it for myself. Below are the results (though I don't know how to interpret them into the actual difference ie 'method 1 is .000001 faster than method 2').</p> <p><strong>Results</strong></p> <p>Test 1</p> <pre><code> var instance:Model = Model.instance; var startTime:int = getTimer(); for(var i:int = 0; i&lt;10000000; i++){ instance.url = ""; } trace(getTimer()-startTime); // 826, 929, 823 var startTime:int = getTimer(); for(var i:int = 0; i&lt;10000000; i++){ Model.instance.url = ""; } trace(getTimer()-startTime); // 3483, 3976, 3539 </code></pre> <p>Test 2</p> <pre><code> var instance:Model = Model.instance; var localLogo:BitmapImage = Model.instance.logo; var startTime:int = getTimer(); for(var i:int = 0; i&lt;10000000; i++){ localLogo = logo; } trace(getTimer()-startTime); // 2070, 2083, 2110 var startTime:int = getTimer(); for(var i:int = 0; i&lt;10000000; i++){ instance.logo = logo; } trace(getTimer()-startTime); // 2028, 2509, 2154 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload