The code is minified, but reformatted is a few hundred lines of code. I'd imagine such a minimal page to have minimal code as well. What is it that Google is doing that the source is this long? I can see a lot of it is javascript, but I was under the impression that inlining so much javascript was bad practice.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
At least one chunk of that code is used for the
ajax
callbacks for Instant Search.Also, when you consider whether inlining Javascript is bad, you should think about the alternative. If the code was in a separate file, it would be another
http
request, which means that the page would load with higher latency.The JavaScript is probably not inlined in their unbuilt source. That would be potential bad practice. By inlining it into the page source when built, the page won't have to make any additional HTTP requests to download a separate resource file, as HTTP requests are one of the slowest parts of loading a web page.
The sheer amount of JS is, by my guess, probably due to a lot of common libraries and code shared among many of their pages, and trying to have it not load on their homepage would end up involving a lot of unnecessary and nasty workarounds in their code. Plus, the UI may look simple, but how much is actually happening under the hood? There are Google accounts to worry about, instant and voice searching to do, analytics tracking, etc.
In the end, however, you probably won't truly know unless you get a Google engineer to divulge their secrets, or get a job with them.
I think this is a principle of the separation of interface and implementation: the user sees the interface and does not need to care about what the implementation is -- how simple or how complicated, and in what way, as long as the implementation does the job of what the interface is supposed to provide.
Take the example of a watch. When you look at your watch, you don't think about the implementation at all. All you care is your can tell the time by the hour and the minute hand, and that you can change to a stop watch mode and button A can start it and button B can reset it -- if it can provide the functionality of a stop watch. The interface can be very simple, and sometimes as simple as possible. The implementation, can be complicated... such as if it has circuitry to sync up with atomic watch signal in various regions of the world, by using millions of transistors to do the job well -- but the user wouldn't usually care what the implementation is. All the user may care is a simple to understand interface of the watch: to look at the watch's face and within the blink of an eye, be able to tell what time it is.
As to why the code can be so big, besides the auto-suggestion mechanism, which includes a throttling mechanism so that not every keystroke will cause a request to Google's server, there are also the Google+ share which has Photo, Link, Video, Event, each with different handling. So although the page can be clean and simple, what the page can provide is a lot more.