Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Google not optimize Date/Time construction in their front-page Javascript [closed]

In JavaScript, we can get a number representing the current date/time like so:

var dt = new Date();
var e = dt.getTime();

A slightly shorter hand way of doing this might be:

var f = (new Date()).getTime();

The most compact way of doing this is reflected in the following code:

var g = +new Date;  //11 bytes shorter

(See them working here: http://jsfiddle.net/es4XW/2/)

Now, when you search through the source code of the Google's front page, you will find the second convention used 11 times. Therefore, it appears that on each occasion Google could save 11 bytes – 121 bytes in total.

Compression and caching will play a part in mitigating this, but surely it would be worthwhile for Google to make this simple switch.

Compare and contrast this with Amazon’s front page code, who do use the third convention (though not on every occasion).

So why aren't Google interested in this optimization? Although 121 bytes is peanuts to most of us, I would have thought they would have been interested in squeezing every last bit of performance out of their front page.

like image 946
James Wiseman Avatar asked Oct 08 '22 00:10

James Wiseman


1 Answers

I'd say readability and thus maintainability over marginal byte savings. Also, the first 2 options are future proof, whereas the third option is an implementation detail which might get changed or works differently across engines (e.g. Rhino, Node).

A 4th possibility to your examples would be to wrap the call into a function:

var now = function() {
    return (new Date().getTime());
}

Of course this only saves a few bytes once you call it at least a few times.

like image 123
Torsten Walter Avatar answered Oct 19 '22 11:10

Torsten Walter