# The best/most efficient actionscript practices thread

**URL:** https://forum.kirupa.com/t/the-best-most-efficient-actionscript-practices-thread/201773
**Category:** flash
**Created:** [September 25, 2006, 1:23am UTC](https://forum.kirupa.com/t/the-best-most-efficient-actionscript-practices-thread/201773 "2006-09-25T01:23:13Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [September 25, 2006, 1:23am UTC](https://forum.kirupa.com/t/the-best-most-efficient-actionscript-practices-thread/201773/1 "2006-09-25T01:23:13Z")

</div>

Ok, well, I haven’t seen a thread like this aroung anywhere so I thought I’d start one. Basically, you just list, and whack up a code wih the best actionscripting practices and/or most efficient.  
Feel free to add on to the list, or provide a faster method of a code given;).  
My first example,

**Function:** searching through an array  
**Type:** the (presumed) quickest method  
**Comment:** The (presumed) fastest way to search an array for a certain value

```auto
Array.prototype.find = function(x) {
 for (var i = this.length; i>-1; i--) {
  if (this* == x) {
   return true;
  }
 }
 return false;
};
var myArray:Array = ["one", "two"];
trace(myArray.find("one"));
trace(myArray.find("three"));

```

**Function:** Key presses  
**Type:** the (presumed) quickest method  
**Comment:** UseASCii codes rather than Key._Key Name_  
So not,

```auto
Key.isDown(Key.SPACE)

```

but

```auto
Key.isDown(32)

```

**Function:** If/Else Strings  
**Type:** the (presumed) quickest method  
**Comment:** Try to keep all your conditions in a string, in the order of the one most likely to be met first, the one most likely to be met second… etc…

**Function:** Increasing a variable by one  
**Type:** the (presumed) quickest method  
**Comment:** Try to keep use variable++ or variable-- as oppose to variable += 1 and variable -= 1.

**Function:** Breaking for/while loops  
**Type:** the (presumed) best practice  
**Comment:** Whenever you are using a for or while loop, if you have any conditionals within it, break or return it after the condition is met.

**Function:** Declaring variables  
**Type:** the (presumed) best practice, the (presumed) quickest method  
**Comment:** Declare them as a variable, and with a datatype. So;

```auto
var myVar:Number = 6;

```

should be processed faster than

```auto
myVar = 6;

```

**Function:** onEnterFrames  
**Type:** the (presumed) best practice, the (presumed) quickest method  
**Comment:** Do not be an onEnterFrame fanatic, chances are if you get addicted to them, you will use them where not necessary. ONLY use them when necessary, they use enough CPU power already

**Function:** Referencing the \_root frame.  
**Type:** the (presumed) best practice  
**Comment:** Try not to use \_root at all throughout your movie. Try to use \_global, \_parent or put this sort of thing on the frame

```auto
var root:MovieClip = this;
myMovieClip.onPress = function(){
root.gotoAndStop(2);
}

```

This will save you a LOT of issues, with regard to referencing when you are going to be using loadMovie or you transfer the frame into a movie clip… etc…

**Function:** Where to put your codes…  
**Type:** the (presumed) best practice  
**Comment:** Try to keep all your coding on frames rather than movieClips… this makes things a lot more flexible for you to do.  
_Note: It also makes things quicker if it’s all in one place, so that flash doesn’t have to cycle through movie clips._

**Function:** Variable names  
**Type:** the (presumed) best practice, the (presumed) quickest method  
**Comment:** Try to keep all your variable’s names short and meaningful.

```auto
var mymassivevariablenamegoeshere:String = "hey";

```

is slower than

```auto
var longVar:String = "hey";

```

**Function:** Performing calculations  
**Type:** the (presumed) best practice, the (presumed) quickest method  
**Comment:** Do your best to perfom calculations only once whereever possible. It’s faster to have

```auto
var myNum:Number = 70005/54667;
onEnterFrame = function():Void {
trace(myNum*2);
};

```

rather than

```auto

```

onEnterFrame = function():Void {  
trace((70005/54667)\*2);  
};

```auto
Doing this also makes things a lot easier to understand and process.
 
**Function:** Frame rate
**Type:** the (presumed) best practice
Comment: Use a nice, smooth frame rate, about 20-30 FPS, but really, it's whatever your happy with.
```
