Gmail Homepage’s Quota estimate
Tuesday, February 26th, 2008I was curious about how the quota counter on Gmail’s homepage (you know, the one before you log in, that’s updated every second) worked, so I had a quick look at the source, and it proved pretty interesting. First, it’s one of the few scripts I’ve seen from Google that’s not obfuscated. Then, it turns out they use linear interpolation between some past storage values and estimated future storage. The joke is that they have a storage estimate of Number.MAX_VALUE in Year 3456

A brilliant summary of how storage estimates evolved over time can be found on Zvi Devir’s page. He also points out the Sci-Fi inside joke comment in the interpolation array definition from the geeks over at Google: // Estimates of nanite storage generation over time.
And finally, a function called updateQuota() is called every second to change the quota value on the webpage via DOM scripting.
Another interesting thing in this script is how they get a value of Round-trip Time (RTT) between your browser and their server and store it in a cookie for future use. They do that by loading an image of size 0, and by looking at the time span between when they start to send it and after they receive it. Well, strictly speaking, depending on your connection’s bandwidth this estimate can be higher because you have to send all the bytes in the request and get all the bytes in the response (when strictly speaking RTT is for one Bit) but I guess the estimate is good enough that way. In my case Gmail’s RTT varies greatly, from values as low as 172ms to as high as 3s. For reference, the code they use is:
var ONE_PX = "https://mail.google.com/mail/images/c.gif?t=" +
(new Date()).getTime();
function LogRoundtripTime() {
var img = new Image();
var start = (new Date()).getTime();
img.onload = GetRoundtripTimeFunction(start);
img.src = ONE_PX;
}
function GetRoundtripTimeFunction(start) {
return function() {
var end = (new Date()).getTime();
SetGmailCookie("GMAIL_RTT", (end - start));
}
}