Activate your free membership today | Log-in

Thursday, July 9th, 2009

Lessons from Gmail: Using Timers Effectively

Category: JavaScript

Neil Thomas of the Gmail for Mobile team keeps coming with great information on lessons learned. The latest discusses timers, and how to use them effectively (setTimeout/setInterval).

There are old questions here.... should you use lots of timers, or use one uber-timer that coordinates life? Here are some thoughts from Neil:

When I first started working on the new version of Gmail for mobile, the application used only a couple of timers. As we continued adding more and more features, the number of timers grew. We were curious about the performance implications: would 10 concurrent timers make the app feel slow? How about 100? How would the performance of many low-frequency timers compare to a single high-frequency timer?

To answer these questions, we conducted a few experiments. We injected some extra code into a development build of Gmail that created a lot of different timers, each of which just performed some simple calculations. Then we fired up the app on an iPhone 3G and Android G1 (both running the latest version of their respective firmware) and observed the performance. Note that although we could have just created a separate test page for this, we chose to inject the code right into our app so we could see how fast it would be to read and process mail with all those timers running.

Here's what we learned. With low-frequency timers — timers with a delay of one second or more — we could create many timers without significantly degrading performance on either device. Even with 100 timers scheduled, our app was not noticeably less responsive. With high-frequency timers, however, the story was exactly the opposite. A few timers firing every 100-200 ms was sufficient to make our UI feel sluggish.

This led us to take a mixed approach with the timers we use in our application. For timers that have a reasonably long delay, we just freely create new timers wherever they are needed. However, for timers that need to execute many times each second, we consolidate all of the work into a single global high-frequency timer.

He then goes on to discuss the nice trick to tell if an application has come back from a sleeping state (and thus, maybe you want to call home and sync):

One neat application of a high-frequency timer is to detect when the device has been woken from sleep. When your application is put to sleep — either because the device is put into sleep mode or because the user has navigated away from the browser to another application — time, as perceived by your app, pauses. No timers fire until the user navigates back to your app and it wakes up. By keeping a close eye on the actual time that elapses between high-frequency timer ticks, you can detect when the app wakes from sleep.

First, create a high-frequency timer, as described above. In your timer, call a function that keeps track of the time between ticks:

JAVASCRIPT:
  1.  
  2. // The time, in ms, that must be "missed" before we
  3. // assume the app was put to sleep.
  4. var THRESHOLD = 10000;
  5.  
  6. var lastTick_;
  7. detectWakeFromSleep_ = function() {
  8.  var now = new Date().getTime();
  9.  var delta = now - this.lastTick_;
  10.  if (delta> THRESHOLD) {
  11.    // The app probably just woke up after being asleep.
  12.    fetchUpdatedData();
  13.  }
  14.  lastTick_ = now;
  15. };
  16.  

Now your users will always have the latest data available when they return to your app without having to manually perform a refresh!

We recently ran into timer fun, as Ben mentioned on his directory post. He put together a nice little timing framework that is DOM agnostic that lets you have some fun with setting frameworks. There are other frameworks out there of course, especially when you get into the DOM (dojo.Animation can be used in the abstract, and there are then DOM specific impls on top).

Robert O'Callahan has been playing around with the idea of a framework in the browser too, with mozRequestAnimationFrame. Fun :)

Posted by Dion Almaer at 6:13 am
5 Comments

++++-
4.4 rating from 33 votes

5 Comments »

Comments feed TrackBack URI

this is a very interesting read for anyone doing continuous updates in the browser, and i recommend reading both http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html and http://ejohn.org/blog/how-javascript-timers-work/ .

i’ve found the single most important piece of advice in this field is, “when you can not be very sure that the code called by setInterval(f,dt0) will finish in clearly less than dt0 milliseconds, consider to use setTimeout(f,dt1) instead (from within f itself to keep the process running)”. this will give at least dt1 milliseconds to your other browser processes to execute so a freeze becomes less likely. you may want to check elapsed time before calling setTimeout from within f so you can add more delay in case execution was swift.

Comment by loveencounterflow — July 14, 2009

With the changing trend of the world, wedding apart from white,Princess Strapless Satin Evening Dress ivory, beige and other traditional colors,are also increasingly popular in recent years, ghetto prom dressespink wedding package,gothic prom dresses such as pink, pink orange, baby blue, purple night, light green and light silver-gray, very soft and pleasing to wedding hair accessoriesthe eye; if you have the courage to try, dark green, purplish red, deep purple, decorated in pink wedding dress, the formation of rich colors,the dress, the effect is very special;Sash Lace Satin Flower Girl Dress, laden with flour colored silk flowers, butterflies, in order to add color.wedding dressIn fact, the wedding is the color of lesser importance, the most important prerequisite is to match the bride’s complexion. Oriental deep and yellowish color, wearing white Mermaid Satin Organdie Wedding Dress,will appear dull, wearing ivory would be more harmonious and natural, baby blue, wedding accessoriespurple night can not be coordinated with the yellow, but pink orange, light green with yellowish color match. As for the skin and rosy, or bronze skin, wearing white will look great, wedding dress

Comment by wuwei — November 10, 2009

A wedding dresses is always

expensive. Every couple will agree on this.When you are planning for

your wedding, you will probably find that every item can be very

expensive. This is especially true when it comes to the wedding dress.
You may think that there is no way for you to get a Wedding gown dresswhich is

elegant and at the same time cheap. However, I can tell you that there

are some ways to save money on this item.
So, the question here is how to make your white wedding dressinexpensive.
In fact, one thing you need to understand is that it is totally

possible for you to find a cheap

wedding dress which is also elegant and beautiful. You do not need

to sacrifice the quality just because you will need to search for cheap wedding gowns items.
As a matter of fact, one of the ways to get a bridal gowns is to wear the one

your mother wore when she got married. The point here is that you will

be wearing a bridal dresses

item and it will certainly be very stylish.

Comment by wuxiseo — November 26, 2009

The principal concern whenever lifting heavy objects on an uneven

surface is to ensure stability. The basic wedding dresses esign premise of

a cantilever hoist is to include a heavy counter weight that opposes

the load and ensures longitudinal seostability in use.
This use of a counterweight is backed up by a relatively wide chassis

on fully lockable 8 inches by 2 Electric hoist inches phenolic

casters.Whenever you need to be able to electric hoist or lift a

weighty object from the floor onto a higher level, having a lifting

system chain hoist that you

can rely on is essential. Unfortunately, there are always situations

where it is simply not possible to use conventional chain block lifting gear such as

jibs or fork lifts, and under these situations, when it is Snatch Block necessary to be able to raise a

weight quickly, then a cantilever hoist is often the best choice.

Comment by wuxiseo — November 26, 2009

Cheap wedding dresses

Comment by 2044060407 — December 14, 2009

Leave a comment

You must be logged in to post a comment.