前段时间看见篇文章是说性能优化的,今天有时间好好看了一下,很多基础的,也有一些比较值得注意的,顺便把主要的一些结论翻译出来,对这方面不是太了解的朋友可以看一下,原文点这里 。
1. Avoid the new operator when creating Arrays
var a = [];
NOT:
var a = new Array();
不要new数组,使用[];
顺便说下FP10支持类型数组Vector,这里 有篇文章可以看下,是说Vector比Array快将近60%。
2. Arrays are expensive to create, do so conservatively
var vanityCollection01 : Array = new Array();
var vanityCollection02 : Array = new Array();
var vanityCollection03 : Array = new Array();
var vanityCollection04 : Array = new Array();
数组是非常耗费资源的,适当使用。
3. Fastest way to copy an array:
var copy : Array = sourceArray.concat();
复制数组最快的方法是用concat()。
4. Setting values in Arrays is slow
employees.push( employee );
employees[2] = employee;
给数组元素赋值是很慢的。
5. Getting values from Arrays is twice as fast as setting
var employee : Employee = employees[2];
从数组getting一个值速度是setting的两倍
6. Use static for properties methods that do not require an object instance
StringUtils.trim( "text with space at end " ) ; |
尽量使用静态属性和函数而不要依赖一个实例
特别是功能类的函数
7. Use const for properties that will never change throughout the lifecycle of the application
public const APPLICATION_PUBLISHER : String = “Company, Inc.”;
使用const申明那些程序里从来不会改变的属性变量
8. Use final when no subclasses need to be created of a class
public final class StringUtils
使用final定义不会有子类的类
9. Length of method/variable names doesn’t matter in ActionScript 3.0 (true in other langs)
someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();
长的变量/函数名是程序的执行是没有影响的。(其他语言也是)
10. One line assignments DO NOT buy any performance (true in other langs)
var i=0; j=10; k=200;
用一行声明若干变量不会提升性能。(其他语言也是)
11. No difference in memory usage between an if statement and a switch statement
if ( condition ) { // handle condition } |
if和switch语句使用时在内存占用上是没有差别的。
12. Rank your if statements in order of comparisons most likely to be true
if ( conditionThatHappensAlot ) { // logic to handle frequently met condition } else if ( conditionThatHappensSomtimes ) { // handle the case that happens occaisonally } |
把为true概率大的条件放到前面。
13. AVM promotes int to Number during calculations inside loops (VM has been changing, from 9 to 10, so int, uint and number conversions aren’t as slow as they used to be.)
int, uint 和 number的转换不像以前那样慢。
15. Use uint sparingly, it can be slow (VM has been changing, from 9 to 10, so int, uint and number conversions aren’t as slow as they used to be.)
保守使用uint
16. Use integers for iterations
for(var i: int = 0; i < n; i++)
NOT
for (var i: Number = 0; i < n; i++)
循环中使用int而不是Number
18. Multiply vs. Divide: instead of 5000/1000 use: 5000*0.001 用乘*代替除/
19. Locally store function values in for and while statements instead of repeatedly accessing them
for (..){ a * 180 / Math.PI; }
declare: toRadians = a*180/Math.PI; outside of the loop
循环外面计算出循环中要使用的值存放在变量中在循环中使用而不是每次循环中去计算
20. Avoid calculations and method calls in loops
var len : int = myArray.lengh;
for (var i=0;i
NOT:
for (var i=0;i< myArray.lengh;i++){ }
在循环外面计算出循环次数(如果是动态的话)而不是每次循环重新计算。
这个问题我以前常犯-_-!!!
21. Use RegEx for validation, use string methods for searching
// postal code validation example using regular expressions |
使用正则表达式来校验字符串,使用String的substring()方法来查找。
29. Remove unnecessary container wrappers to reduce container nesting
使用最少的容器嵌套。