This is part three of my Widget Anatomy series which which will explain the ins and outs of the Widget Framework that is shipping as part of Windows Mobile 6.5. In this installment I will discuss how to squeeze the last drop of performance out of the Widget framework to ensure all our Widgets are interactive, alive and fun to use while being good mobile citizens and not draining the user’s battery dry in the process.
All good things
I know that this sounds like a big obstacle to overcome but it is really not that bad.
One Small Step
On very interesting aspect of performance is that, in most cases, perception is reality. In a mobile application world, this means that for every user action there is a immediate response from the application that assures the user that their input was acknowledged and is being processed.
The best way to accomplish this on widgets is to minimize the amount of code that is executed on the event handler and defer all actions that are not related to updating the UI to give feedback to the user after a small timeout.
The same technique can be applied to break out expensive operations to allow processing of the always important user events.
Another trick that will come handy for most Widgets since they are generally full of dynamic content is that using innerHTML is significantly faster than creating the DOM elements individually and then appending them to the document. You should never append them to the document one by one, as this will completely destroy your performance since it will force the Widget to re-layout multiple times — and this is really slow!. I know that this might not be possible in all cases but it is a nice tool to have in case your widget is having performance issues creating dynamic content.
Now let’s focus our attention to the flip side of performance which is, without a doubt, battery life. It is a fact of life that our Widgets, since they are executing code on the device, will have an impact on battery life. The goal is to minimize its effects to ensure happy users.
Thankfully the road to user happiness is not as rough as it might seem, there are a few principals that, if we keep them in mind while writing our Widget code, then we will know for sure that we are only using the power that is necessary to complete our task.
- Only consume network resources when your widget is active.
- Minimize animations when the widget is idle.
- Cache data locally as aggressively as possible, use the widget persistent storage as a repository since it will be preserved across runs (and even upgrades).
- Be aware of the current battery level and minimize network operations when the battery is running low.
- Always (and by that I really mean always!!) use asynchronous network requests.
That’s it for now, but before I completely forget, one set of tools that will be your friend tracking performance issues on your widgets (and web applications as well) is the IE8 amazing developer tools (to start them, just press F12) which include a script profiler and it works great for widgets!
Next post: Widget Anatomy – Security