actionscript_改善ActionScript性能的技巧,窍门和最佳实践

actionscript

Here are some practices and techniques that can be adopted into your Flash/Flex application development process.

这是可以在Flash / Flex应用程序开发过程中采用的一些实践和技术。

Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data, does not cite sources, and does not indicate 注意:并非所有的“性能提示”都能立即为您带来好处。 本文不包括时序验证数据,也没有引用源,也没有指出每个项目的 relative performance gain for each item. Nevertheless, it may be useful as a general guide.  For time-critical operations, I recommend that you set up your own performance tests to verify the accuracy and applicability of these suggestions.

1个 (1)

Avoid the new operator when creating Arrays.

创建数组时,请避免使用new运算符。

var a = [];            /// is better than
var a = new Array();

2 (2)

复制数组的最快方法是:
var copy : Array = sourceArray.concat();

3 (3)

Use static for properties methods that do not require an object instance.

对于不需要对象实例的属性方法,请使用static

StringUtils.trim( “text with space at end ” );
Class definition:

package {
public final class StringUtils
{
    public static function trim( s : String ) : String
    {
    var trimmed : String;
    // implementation
    return trimmed;
}}}

4 (4)

Use const for properties that will never change throughout the lifecycle of the application.

const用于在应用程序的整个生命周期中都不会改变的属性。

public const APPLICATION_PUBLISHER : String = “Company, Inc.”;

5 (5)

Use final when no subclasses need to be created from a class.

当不需要从类创建子类时,请使用final

public final class StringUtils

6 (6)

方法/变量名称的长度在AS3中无关紧要(某些其他语言中为true)。
someVeryLongMethodNameDoesntReallyImpactPerformanceMuch();

7 (7)

一线作业不购买任何演奏(在某些其他语言中为真)。
var i=0; j=10; k=200;   // ... is no faser than if on separate lines

8 (8)

There is no difference in memory usage between an if statement and a switch statement:

if语句和switch语句在内存使用方面没有区别:

if ( condition ){
    // handle condition
//--------------------------------------- IDENTICAL MEMORY USAGE:
switch ( condition )
{
    case “A”:
    // logic to handle case A
    break;
    case “B”:
    // logic to handle case B
    break;
}

9 (9)

Rank your if statements in order of comparisons most likely to be true

根据比较的if语句进行排名

if ( conditionThatHappensAlot ) {
    // logic to handle frequently met condition
} else if ( conditionThatHappensSomtimes ) {
    // handle the case that happens occasionally
} else {
    // handle the case that doesn’t happen that often
}

10 (10)

AVM promotes int to Number during calculations inside loops.

AVM在循环内的计算期间将int提升为Number

11 (11)

使用整数进行迭代:
(var i: int = 0; i < n; i++)        // is better than...
(var i: Number = 0; i < n; i++)

12 (12)

处理索引时,使用值-1表示“无索引”。

13 (13)

Locally store function values in for and while statements instead of repeatedly accessing them.

forwhile语句中本地存储函数值, 不是重复访问它们。

var toRadians:Number = a*180/Math.PI;
for (..){ b* toRadians; }

14 (14)

影格速率

Despite all discussions otherwise, there is no magic framerate. I use 25 or 30 because I like it best. At some point, I tested and determined one was slightly better than the other, but generally speaking this is not going to be the primary cause of a site running slow. I wouldn’t generally advise going higher than 30 though, just because you’re asking the player to render an awful lot awfully quickly…

尽管进行了所有讨论,但没有神奇的帧率。 我使用25或30,因为我最喜欢它。 在某些时候,我测试并确定一个站点比另一个站点稍好一些,但是总的来说,这并不是站点运行缓慢的主要原因。 我一般不建议高于30,只是因为您是在要求玩家Swift渲染出很多可怕的东西……

15 (15)

尽可能使用ENTER_FRAME事件而不是Timer事件。

16 (16)

visible is better than alpha.

可见优于alpha

alpha = 0 and visible = false are totally different things. Alpha determines the opacity of a clip. Visible determines whether or not the clip should actually be rendered by the player. If you’re setting something all the way invisible, use the visible property.

alpha = 0和visible = false是完全不同的东西。 Alpha确定剪辑的不透明度。 可见确定剪辑是否应该由播放器实际渲染。 如果您将某项设置为完全不可见,请使用visible属性。

17 (17)

Use try...catch wherever possible.

尽可能使用try ... catch

Each try, catch, and finally (optional) represent three different types of code blocks that can be used within a try..catch..finally statement.

每个try,catch和finally(可选)表示可以在try..catch..finally语句中使用的三种不同类型的代码块。

18 (18)

乘除法:

For instance, use – 5000*0.001

例如,使用– 5000 * 0.001

instead of – 5000/1000

代替– 5000/1000

19 (19)

使用RegEx进行验证。 使用字符串方法进行搜索。

20 (20)

重用对象以维持“内存平稳期” DisplayObjects,URLLoader对象。

21 (21)

遵循Flex组件模型中的自定义组件以获得更好的性能:
createChildren();
commitProperties();
updateDisplayList();

22 (22)

避免使用setStyle()方法-这是Flex框架中最昂贵的调用之一。

23 (23)

尽量避免将Repeaters用于可滚动数据。

24 (24)

cacheAsBitmap and BitmapData

cacheAsBitmapBitmapData

Where possible, use cacheAsBitmap to rasterize vector items. You’ll force Flash to draw the symbol one time and then never again. On the flip side, if you’re scaling or rotating a symbol, NEVER set it to cacheAsBitmap. Then you force Flash to render AND recapture the raster every frame, making the process slower instead of faster.

尽可能使用cacheAsBitmap栅格化矢量项目。 您将强制Flash绘制符号一次,然后再绘制一次。 另一方面,如果要缩放或旋转符号,则切勿将其设置为cacheAsBitmap。 然后,您强制Flash每帧渲染和重新捕获栅格,从而使过程变慢而不是更快。

25 (25)

将向量用于固定数据类型的数组

I highly recommend reading this very thorough Flash Player 10.1 Performance review in several parts by Jackson Dunstan, an AS3 programmer. It describes similarities and differences in all major aspects of performance between Flash Player 10.0 and 10.1, documented by data from additional articles.

我强烈建议阅读AS3程序员Jackson Dunstan撰写的非常详尽的Flash Player 10.1性能评估,分为几个部分。 它描述了Flash Player 10.0和10.1在性能的所有主要方面的相似性和差异,并通过其他文章中的数据进行了说明。

Flash Player 10.1 Performance: Part 1 Flash Player 10.1性能:第1部分 Flash Player 10.1 Performance: Part 2 Flash Player 10.1性能:第2部分 Flash Player 10.1 Performance: Part 3 Flash Player 10.1性能:第3部分

Note: This article is reprinted from my blog.

注意:本文转载自我的博客

翻译自: https://www.experts-exchange.com/articles/4195/Tips-Tricks-and-Best-Practices-for-Improving-ActionScript-Performance.html

actionscript

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值