How to improve the performance of Flex App? - apache-flex

I am working on a flex application.The application occupies 100% of the browser screen, but it is slow to load.How should i improve the performance, what are the best techniques one should follow for improving the performance of a flex App.
I did try some stuff though, i am not using relative layout instead i am using absolute layout and putting the buttons/images etc at exact places in a Canvas, still i feel it is slow.Can i profile the App or flex code to see where exactly the app is taking the maximum time to execute.

Adobe offers a great article about this topic: Flex Application Performance: Tips and Techniques for Improving Client Application Performance

Here are some great resources on improving performance of not only your Flex app, but of Actionscript in general:
Quick as a Flash Presentation by Grant Skinner - Optimizing Actionscript
Round up of Actionscript and Flex Optimizations
Flex Application Startup Performance
Actionscript Performance Tuning
Overall, some key things to always do:
Keep nesting thin: Don't nest too many VBoxes and Canvases, that will make your app dramatically lag/freeze
Don't Embed: Only embed things that are very small and need to show up immediately (icons in skins for example)
Deferred Instantiation: Only add what the user needs to see. Everything else will wait until the user goes there.
Explicitly size all item renderers. If DataGrid item renderers aren't explicitly sized, your app might freeze for an instance when constructing it.
Optimize Assets for the Web (icons ~< 20k, large images ~< 200k as a rule of thumb).
Refactor to remove excessing curly-bracket Binding (width="{something.other.width/4}"). Causes a bunch of events to be dispatched that you likely don't need. Too many events == too many temporary objects == lag.
People recommend using Modules to decrease swf size and only load what you need, but if your application isn't as large as a CMS, you generally don't need them.

Flex Builder Professional has a built-in profiler that works pretty well for profiling your own code. If the problem is drawing performance and layout, it's not as helpful (it profiles the code just fine, but it's harder to understand the results and apply them in a useful way to changes in your code).

Related

Why people write Flash video players in Flex not in Flash?

One common hebavior I observed in my slow Internet connection is that most sites on the Web embedding Flash Video, their player is always written in Flex. I can tell by the extremely long loading scroll bar that Flex defaultly provides. From my experience Flash loads faster, Why do people stop writing stuff in Flash anymore?
Are there any fast loading yet rich feature Flash video players>
My experience is that the components provided by default in the Flex framework are more stable than the ones provided in the Flash IDE. So I'd prefer to use the Flex components simply because it means less time debugging problems in the actual component code. It is however true that players based on the Flex framework tend to be heavier in terms of download size. But since video is bandwidth heavy and people who watch video over the net tend to have good bandwidth these days, I guess the conclusions most developers come to is that the extra download size is an acceptable tradeoff for less time spent in coding boilerplate.
Personally, if the requirements state that the player has to load fast and be light weight then I always roll my own in pure AS3 and just implement exactly as much as is needed. But if there is no such requirement, then I'll use the Flex-components as a base and do customization from there.
As for the second part of your question, sorry can't think of any open source fast loading feature rich flash video players right now.
Flex produces a flash movie (swf), so the end result is still flash.
As for the reason behind it, not everybody has or wants Adobe Flash (Studio) or any of the other timeline based studios.
Flex enables anybody to create a flash based application or widget using XML and the free Flex SDK.
There may also aesthetic reasons such as standardised controls.
Try Flowplayer, JW FLV Media Player and the last contender, but not the least from Adobe - Strobe Media Playback http://www.osmf.org/strobe_mediaplayback.html
From my experience Flash loads faster, Why do people stop writing stuff in Flash anymore?
When done correctly, there's no difference in how they work - in fact, I'd argue that stuff done in Flex (specifically, using the Flex SDK) give you more freedom to control how loading is done.
But to answer the question, people stopped using Flash just because there's much better stuff out there. Flex Builder, FlashDevelop, FDT - they're all much better tools for any serious coding and debugging. I used to love the IDE, but now I can't fathom how would anyone do anything serious on it, even when using external code editors.
Flash still works when you need vectors, or to create a library with some embeddable assets, but that's pretty much all it's useful for nowadays.
I am a flex developer, see flex is somewhere flash, here flex has two frames, preloader stage(first frame) and creationComplete stage(second frame), the same frame concept as flash has, but flash has more than two frames and layer concept is also there
Major differneces comnes in the ease of using the components in flex, in flash, altho flash is one of powerful tools that has changes the web,
but flex gives the freedom, i m using flex, so i know that i can give more time on business login development, rather than concentrating on design aspects,
but it's also true, i have to see the design aspects as well in flex,
so happy flexing

How to reduce SWF filesize by optimizing the code?

Considering we have already done the following actions:
Flex Framework as RSL
Compiling with debug=false
Loading most images at runtime
Drawing other simple images with flash draw features
Reducing complex images with pngquant
Creating modules for secondary features
Applying ranges to fonts
Running FlexPMD to find dead code and bad copy-paste
Running FlashOptimizer and secureSWF (with poor results)
Today our application is 1358k:
Code: 978k - 72%
Images: 270k - 20%
Fonts: 110k - 8%
We believe we spent a lot of time into asset optimization and most of the work is remaining on the code.
By analyzing our link-report, our guess is that the heavy part of the code is comming from Flex .mxml nested components. We don't think there is much to do on our pure AS classes.
Is there any analysis or coding best practice in order to reduce the impact of the code on the swf filesize ?
Thanks.
Here is the application : http://www.pearltrees.com/nicolas/137698/
In my practice I usually don't have big final swf files, so I want to mention only one thing. Using mxmlc directly we should not forget to add (for the final build of course) parameter/attribute
debug = "false"
in other way final swf will be almost 2 times bigger.
do you have an objects in the .mxml that are similar to each other that you could turn into a generic class and customize programatically?
Consider looking into preloaders and modules.
Without knowing your application, it's hard to be specific, but a custom preloader can sometimes help a lot with perceived download time. Let's face it, asking the user to idly stare at a progress bar is sad, and you can do better.
The usual example here is that your need your application users to login, or select some basic details before jumping into the main application. By implementation that first form as a preloader, your application will keep downloading in the background while your user interacts with that form.
The downside: Your preloader code doesn't have access to all the Flex goodness. You'll have to draw your UI and implement your interaction in plain old AS3. Still, the extra work can be worth it in some situations.
Flex Modules are the other thing that'd be worth looking into. In a complex Flex app, not everything is commonly used. If you cut the lesser-used bits from the main application and move them into a module you load on-demand, you may be able to save a fair amount of bytes from the initial download size.

Flex projects versus Actionscript projects

I have used Flex for about a year before deciding that I would rather develop Actionscript projects.
At the time, it seemed that the framework was too heavy for the kind of work I was dealing with, mainly small web applications , personal sites, portfolios this sort of thing. I also thought that Flex was like a odd hybrid , something targeting seasoned developers but at the same time , adding some function wizards that seemed to target beginners. It seemed overly complexed in some areas and way too basic in others.
On the other hand, the IDE was great , definitely no comparison with Flash CS IDE , so for me it made sense to stick to AS3 projects and use Flex , now FlashBuilder to write my code.
( I need to point out that I'm not a Flash designer, so working with Flash CS was never an option. )
It's been a while since I had a look at the Flex framework and I'm wondering about other Flex/Flash developers position on this issue.
Would you only consider Flex for enterprise level projects? What are the advantages of using one over the other? If you were a Flash developer and moved to Flex, what were your motivations? If you're creating both Flex & Actionscript projects , what are your choice criteria?
Edit:
Although I have received a great answer, I would have been interested to hear from Flex's users, what's your main practical motivation ( as opposed to philosophical :) ) for using Flex over pure Actionscript projects?
Preamble: my experience is primarily with AS3 projects built using a combination of the Flash IDE (FIDE) and Flash Builder 4 (FB4).
I generally prefer pure AS3 (PAS3) projects over Flex projects for the following reasons:
Size - Flex projects have a much larger minimum size than PAS3 projects. Not suitable for lightweight applications.
Performance - Flash is not known for its performance, and the layout computations required by a complex Flex application will hammer the end-user's machine. To them, things just end-up feeling slow, non-responsive, or "gunky". Unfortunately, this means that the applications where Flex might be most attractive (i.e. a very complex, adaptable, UI) are the exact places where it stumbles. In the end, you end up writing all this bizarre performance-enhancing optimization code that takes away most of the time you gained from using the built-in layout system.
Metaphor and Appearance - Flex aims to allow developers to provide end-users with a mature, flexible UI that has the same widgets and widget behaviors that they are used to from native applications. However, due to the performance problems echoed above, the UI never feels quite as nice or responsive as a native app. In addition, it's missing all of the OS-specific peculiarities that end-users are used to and will expect. I don't really understand the motivation to try to emulate native app development or behavior - you're never going to win that fight. Best to make something that stands by itself, which is what most native web applications are doing.
Flexibility - Dovetailing into the previous argument, Flash's main advantage is its ability to do things that traditional UI widget libraries can't do (at least not very easily). You can make some really, really novel UIs in Flash that just aren't practical to do in native apps without mucking about in OpenGL. Using Flex makes creating novel UI hard again (but it does make creating standard UI much easier, even if it is, in my opinion, sub-standard UI).
I'm curious if anyone has some good examples of Flex being used in any popular, public websites. Grooveshark is the only one that I know about (which is quite nice, but suffers from many of the problems I've outlined, especially on OS X where Flash performance is still poor).
However - it's a tradeoff. Always remember that your time is valuable. Your users might accept a slightly clunky, slightly confusing interface if it lets them do really cool things and that would mean that you could release it now as opposed to later. This brings is to the major downsides of PAS3 development:
Effort vs. Reward - You have to program all of your own UI. All of it. This can lead to some really, really bloated code where you have to define tons of event listeners for every button you want to create. I don't know how many times I've written various kinds of layout code specific to what I was working on. You can try to write your own abstract classes for these (which I have done), but at some point you're just going to end up re-implementing the Flex framework. Hardly worth your time.
Development - You can either use just Flash Builder 4, in which case you have to construct every graphical asset by hand in code (which takes forever), or you use the FIDE, in which case you can make lovely artwork but you're stuck with a stone-age code IDE and it takes forrrrrreeeeeeevvvvver to compile anything. Currently I use a hybrid setup where art generated in the FIDE is automatically imported into my FB4 project, but even that is not a perfect solution. They really need to be integrated better.
Another set of things to keep in mind: things that Flash sucks at.
Flash sucks at text. Do not try to re-implement a web browser inside Flash. Flash is actually quite good at displayed relatively small amounts of text that is unselectable (and, through the use of embedded fonts, is always pixel-perfect), but don't try to create large, expansive text documents inside your Flash project. First, performance will be terrible, and second, users will expect the text to behave the same way all other large text fields do in their native applications (most specifically, their web browser). Selecting text in Flash doesn't feel right because it doesn't feel how your OS does it.
Flash doesn't play nice with mouse and keyboard input - it constantly fights with the enclosing browser for focus. If your system needs either of those things, users need to click on it first. Don't fall into a trap where people will get confused because their inputs are going to the wrong place.
Flash is a performance hog - we've all heard this one, and it's not nearly as much of a problem as people like to think, but it does mean that you'll have to put a lot of thought into the performance of your system. Your UI should run at a stable 60FPS when being used and should not use much if any CPU when the user is not interacting with it. If your FPS dips below 60, then your UI will feel slow and gunky compared to native or HTML5 UI. Also make sure to watch for memory leaks.
In the end: user your head. Both approaches are just tools in your arsenal.

Disadvantages of a Flex project vs an Actionscript project?

I've recently started making a game in FlexBuilder. The game is currently a Flex project.
Is there any downside to using Flex as opposed to just Actionscript?
A friend of a friend told me that Flex is slower than an Actionscript project. I've been unable to validate this on the internet; is there any truth to that claim?
Thanks!
If you're developing a game, you should be using an ActionScript project. Flex is to be used only for data driven applications and user interfaces.
The flex compiler generates a lot of intermediate code to convert mxml files into actionscript (you can view those files if you compile with -keep compiler option). This code, the flex framework incorporated to your SWF, adds significantly to the size.
Create an actionscript project with a textfield ("hello world") and a flex project with a label with the same text. Build them, go to their bin-debug folders and compare the file sizes. While actionscript one is only a couple of kilobytes, flex swf would be at least a 100 kB.
As far as speed is concerned, since the flex framework is sitting on top of actionscript, it obviously would have a performance downside.
The beauty of flex lies in the easiness to create UI components and developing data driven applications that frequently communicates with the server.
Don't use it unless you truly need it.
If you know flex and it helps you developing faster - do it in flex.
Download size doesn't matter for game with a lot of assets. Most popular flash games have size of 5 MB and more. (for example on kongregate)
Crucial part of game you can make in pure actionscript. Placing hundreds UIComponents to Canvas could doesn't slow in Flex, but Flex rending technology prevent smooth animation of objects.
Conclusion:
Flex is for rapid development. You can use it's easy skinnable components for menu. Even without Flex UI components binding is matter and makes life easier.
Download size = speed on the Internet. The smaller you can make your game, the faster it will load. Size also equals speed in an interpreted language like ActionScript, where the less code you have to execute, the faster it will run. Hand-coding an ActionScript routine might let you make it faster than the generalist approach of Flex.
That said, maybe you'd be willing to pay that overhead to avoid having to write a lot of utility classes in pure ActionScript. Your high-score screen would be easier to do in Flex, for example, and that might be worth the overhead to you. It won't matter if your high-score screen is a little slower, since it isn't a real-time thing like the actual game.
Also consider the cost of your time. By using Flex, you'll be done with those parts of your app faster than if you hand-code them in ActionScript. Unless your time is free, you should be thinking about how this trades off against the bandwidth cost of the Flex overhead. It might be that it's cheaper to pay the bandwidth than your time making a more efficient program.
A Flex Application won't necessarily run slower than a Pure AS3 application once it's fully loaded - everything gets compiled down to bytecode in the end, and a Flex App is like an AS3 app that uses a LOT of other classes.
Think of the Flex Framework as a set of shortcuts that allow you to do things much more quickly, but the real cost is that your project gets filled up with a LOT more code - even if you're actually writing less code, and you never have to see the additional code.
I would disagree with the assertion that Flex should only ever be used for complex data-driven applications, though it's certainly very useful for such projects. You can use it for anything, as long as you understand that the final product (the .swf you export) is going to be a lot larger than it might otherwise be.
If having a large .swf is not that big a deal to you (and it might not be, depending on what you're doing with it) then I'd say give Flex a try because ArrayCollection, RemoteObject and Data-Binding will save you hours of frustration and hundreds of lines of code.
However, if you want to make sure your final app is as small and efficient as possible, do it in pure AS3 and simply opt-in to more advanced libraries as you need them.
Flex provides a framework for building Rich Internet Applications. If your game requires a complicated GUI (such as an RPG), it might be useful. Otherwise, it adds layers that will complicate things if all you want to do is build a game.
As said above in the first point the only beverage is memory consume. But if u write in pure AS script you need to be in deadline combat. As flex provide lots of customizable comps. U can use Class files instead of MX Comps that save the Memory. Do not Create any components unless it is being used. Modules and RSLs are avail to acheive the peretainity

Flash versus Flex

I've tried looking everywhere for a concise list of the advantages and disadvantages of using Flex vs. Flash.
Coming from a programming background, I absolutely love Flex. It's easy to pick up, and since it can use flash classes, why would I want to use Flash without flex?
Flex:
Pros:
good for RIA development
provides many user-input options out of the box
Build in lay-outing system
the MXML is easier for non-programmers
You can quickly combine components to create small applications
components can provide an advantage in large-scale projects because of their modular
nature.
can be developed using linux
has a nice component lifecycle for validation, etc.
Cons:
increases the size of your .swf
Customizing the look of components can take a lot longer than anticipated, depending on the visual style you're looking for
when you find out you need a custom component that doesn't exist, you might need to go back to Flash to do the real programming work and packaging of the component
The "flexibility" of Flex means you will be reading a lot of documentation
Bugs in the Flex framework
You eventually will need to compromise with the architecture of the Flex framework
Flash
Pros:
good for making movies/animations
Timeline can be easier for designers/animators to conceptualize
when working from scratch, provides a great deal of control.
easier for someone with a programming background
You can program whatever you like; no compromises with existing frameworks
Cons:
only provides basic user input (text box) out of the box.
timeline can be daunting for programmers (although you can quite safely ignore it)
Development of certain types of applications will be slower than with Flex
can't be developed using linux
user input validation must all be handled in the code. No built-in validation.
need to implement your own lay-outing system
Please correct me if I missed anything said so far.
Flash and Flex both use the same underlying rendering engine, just with different front-ends. Flash is better suited for making movies and animations. Flex is better for application development.
From a programmer's viewpoint, the big difference between Flash and Flex is not so much which IDE/application you use for programming, but whether you program in ActionScript (AS) only, or use the Flex framework and MXML to program your applications.
I would say pure ActionScript is better for programming (whether you use Flash IDE or Flex IDE is not that relevant), and MXML is better for non-programmers to combine the components programmed in AS.
I would add to your list these pros/cons:
Flex:
Pros:
Easier for non-programmers to get into application development
You can quickly combine components to create small applications
Components can provide an advantage in large-scale projects
Cons:
Customizing the look of components can take a lot longer than anticipated, depending on the visual style you're looking for
When you find out you need a custom component that doesn't exist, you might need to go back to Flash to do the real programming work and packaging of the component
The "flexibility" of Flex means you will be reading a lot of documentation
Bugs in the Flex framework
You eventually will need to compromise with the architecture of the Flex framework
Flash (or Flex IDE in ActionScript project mode):
Pros:
Easier for someone with a programming background ;)
You can program whatever you like; no compromises with existing frameworks
Cons:
Timeline can be daunting for programmers (although you can quite safely ignore it)
Development of certain types of applications will be slower than with Flex
In short: pick the right tool for the right task.
Flex is a library of code written in ActionScript3, so it adds lots of capabilities and standard-library-like stuff to Flash. The downside is that it a is a huges amount code that gets included into your application. If you use any Flex at all in your app, the download size of the SWF goes up by 100's of K.
If your application has any kind of user interface widgets, then you almost have to use Flex as Flash itself only has the most basic things like text boxes. Flex has a whole XML GUI with layouts, data binding and XML setup etc.
Doing that in flash, you end up having to write from scratch things like list boxes...
In my opinion, the most important feature of the Flex framework is the component lifecycle, which provides a really elegant model for validation/invalidation of properties, component size, and hierarchical rendering.
The benefit to developers is that it creates discrete application phases for business logic and rendering, avoiding expensive geometry & rendering code until the last possible moment before drawing a frame.
Here's a really good presentation, explaining how it works:
http://tv.adobe.com/#vi+f15384v1002
The model is so well-designed that the component lifecycle remains almost entirely invisible during the majority of Flex development, when you're using the framework default components and containers. You only need to learn the inner-workings when you start developing your own components.
Developing in the Flash environment, or in pure Actionscript, you don't get any of that. Anyone developing pure AS3 applications either needs to code very carefully to separate business logic from rendering, or will suffer severely decreased performance.
[...] why would I want to use Flash without flex?
Flex is a new product, whereas Flash existed from the Macromedia days. Designer, animators and most anybody who is not brought up on a staple diet of programming education will probably find Flash easier to master than most other such solutions.
Target is different.
Flex is more dedicated for programmer while Flash is more friendly to Artist / web designer.
Flash is the IDE used (generally) to create animations and things that work well on a timeline.
Flex works better for creating internet applications which have interactions more akin to a desktop.
Why use Flash? Well, if you need to do something more specifically attached to a timeline, of course!
I see Flex as more of a solution for doing RIA applications where you need to develop application based solutions. There's quite a lot you get right out of the box with using Flex but it also comes at a price in terms of file size, granularity, etc.
If on the other hand you are working on a totally custom solution such as a game then perhaps Flash is the way to go because you can start fresh with a blank canvas. Many people still use Flash because they don't need all the app based bells and whistles of the Flex platform.
I like the freedom of Flash, and its really simple to embed assets in Flash, a little more confusing to do in Flex.
One thing that I love about flex is the ability to make a fluid application with minimal effort. Which would take forever in Flash.
Anything you can do in flex you can do in flash, just may take a lot longer to do. You can't do everything in flex that you can do in flash though.
Flex takes care of all the UI programming for you and lets you focus on the business logic, with flash you will spend a majority of time programming the UI.
You can develop Flex applications under Linux easily but with Flash you simply can't.
Another solution that was not suggested at here, will be to use them both. You can add flex components to flash movie clips using ContainerMovieClip. And you can add flash movie clips to flex components using SpriteVisualElement. Another thing that wasn't mentioned was lay-outing your application. It will be flex pros against flash disadvantage, because you got build in flex lay-outing system. But again when you are using them both, you can layout your movie clips with flex lay-outing system.
Also flex become Apache top-level project. And it become more and more excepted by the community now.
Flash and Flex are 2 complete different things, one is a design tool with support for action script, the other one is a framework that also has action script but is maily built around MXML which is a XML based UI definition "Language".

Resources