Widget Anatomy – Performance and Battery Life

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

JavaScript is a cool language. It’s simple to learn, use, and understand but it really can get us into a lot of trouble if not used wisely. this is true on all platforms but we must pay special attention to its implications while developing for a mobile platform.

So, just because we are among friends here I’ll share a little secret that showcases the biggest challenge to creating responsive Widgets.  Here it goes… are you ready?… On Windows Mobile 6.5, JavaScript execution blocks the UI.  This means that, whenever there is a script running nothing else can be processed in the widget which includes menu commands and keyboard or touch input processing. Also note that, during script execution the Widget can’t redraw – in other words, any screen updates done during the execution of the script will not be reflected on the screen until after the script finishes its execution.

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.

   1: function onCoolActionClick() {
   2:     document.getElementById('waitCursorLayer').style.display = "block";
   3:     window.setTimeout("doCoolStuff()", 50);
   4: }
   5:  
   6: function doCoolStuff() {
   7:     countToAMillion();
   8:     document.getElementById('waitCursorLayer').style.display = "none";
   9: }

The same technique can be applied to break out expensive operations to allow processing of the always important user events.

   1: function countToAMillion(currentCount) {
   2:     // Count to a million in 10 incremets to ensure the UI events can be processed
   3:     var countBlockStarts = currentCount;
   4:     var countBlockEnds   = countBlockStarts + 10;
   5:     
   6:     for(var i = countBlockStarts; i < countBlockEnds; i++) {
   7:         
   8:         // Check to see if we are done
   9:         if(i >= 1000000) {
  10:             return;
  11:         }
  12:         
  13:         // Write the current count
  14:         document.getElementById("currentValue").innerHTML = i;
  15:     }
  16:  
  17:     // schedule the next few elements
  18:     window.setTimeout("countToAMillion(" + countBlockEnds + ")", 50);
  19: }

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.

Awakening

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.

Example:

   1: function onShow() {
   2:     // The widget was activated and is running in the foreground
   3:     // refresh the widget data
   4:     refreshWidgetData();
   5: }
   6:  
   7: function onHide() {
   8:     // Stop al recurring timers to be a good Widget citizen
   9:     stopAllTimers();
  10: }
  11:  
  12: function onLoad() {
  13:     var systemState = widget.createObject("SystemState");
  14:     var batteryStrength = systemState.PowerBatteryStrength;
  15:  
  16:     batteryStrength.addEventListener("changed", optimizeNetworkUsage);
  17:     
  18:     widget.onshow = onShow;
  19:     widget.onhide = onHide;
  20: }

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


Trackbacks

  1. Posted by: Windows Mobile Blog on September 22, 2009 at 2:00PM

    Previous post: Widget Anatomy – The Manifest This is part two of my “Widget Anatomy” series which will

  2. Posted by: Twitter Trackbacks for Widget Anatomy ??? Performance and Battery Life - Windows Mobile Blog - The Windows Blog [windowsteamblog.com] on Topsy.com on September 22, 2009 at 2:13PM

    Pingback from  Twitter Trackbacks for                 Widget Anatomy ??? Performance and Battery Life - Windows Mobile Blog - The Windows Blog         [windowsteamblog.com]        on Topsy.com

  3. Posted by: Windows Mobile 6.5 Widget anatomy: Performance and Battery Life « MobileTechWorld on September 22, 2009 at 5:11PM

    Pingback from  Windows Mobile 6.5 Widget anatomy:  Performance and Battery Life « MobileTechWorld

  4. Posted by: Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Scripts Rss on September 22, 2009 at 7:26PM

    Pingback from  Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Scripts Rss

  5. Posted by: Strong Women ??? Weight Lifting has a Sweet Upside: Strength … | Fitness Health Wisdom on September 23, 2009 at 3:29AM

    Pingback from  Strong Women ??? Weight Lifting has a Sweet Upside: Strength … | Fitness Health Wisdom

  6. Posted by: South Glamorgan related services in North Yorkshire on September 24, 2009 at 7:33AM

    Pingback from  South Glamorgan related services in North Yorkshire

  7. Posted by: Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Mobile CHN on September 24, 2009 at 2:23PM

    Pingback from  Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Mobile CHN

  8. Posted by: wotudo on October 01, 2009 at 7:38AM

    I had two sessions to present at Over The Air 09 . The first was all about the new Widget runtime included

  9. Posted by: Delving Into Dynamics on October 07, 2009 at 1:32PM

    In part 3 of his series on Widgets for Windows Mobile 6.5, Jorge Peraza talks about how to write great

  10. Posted by: Widget Anatomy ??? Security Insights - Windows Mobile Developer Blog - The Windows Blog on October 17, 2009 at 12:52AM

    Pingback from  Widget Anatomy ??? Security Insights - Windows Mobile Developer Blog - The Windows Blog

  11. Posted by: You Get The . Info » Widget Anatomy ??? Security Insights – 1175th Edition on October 21, 2009 at 2:22AM

    Pingback from  You Get The . Info » Widget Anatomy ??? Security Insights – 1175th Edition

  12. Posted by: Technology Info » Widget Anatomy ??? Security Insights on October 21, 2009 at 7:04AM

    Pingback from  Technology Info  » Widget Anatomy ??? Security Insights

  13. Posted by: Widget Anatomy ??? Security Insights | Voyager Enterprises on December 11, 2009 at 9:39AM

    Pingback from  Widget Anatomy ??? Security Insights | Voyager Enterprises

  14. Posted by: MSDN Flash Podcast 019 – Paul Foster discusses Windows Mobile Widgets « IUpdateable from Eric Nelson on January 18, 2010 at 5:23AM

    Pingback from  MSDN Flash Podcast 019 – Paul Foster discusses Windows Mobile Widgets «  IUpdateable from Eric Nelson