Tech —

JavaScript slows down the Internet; WebKit to the rescue

External scripts can get in the way of loading web page elements in parallel …

Despite increases in CPU and broadband speeds, the 'Net stubbornly refuses to get much faster. The WebKit team behind Apple's browser Safari has found an important contributor to the Web's slowness: external JavaScripts that are part of many web sites get in the way of a browser's ability to load in parallel the different elements that make up a page. This means that browsers—and users—spend most of their time waiting for packets to flow to and from remote servers, which, even at the speed of light, takes a considerable amount of time. Addressing this issue made WebKit almost twice as fast in some cases.

The team did some tests with the Wall Street Journal's front page, which serves as a good example of today's complex web pages. With no speed limit or additional delay, the WSJ page loaded in about three seconds. By adding 50 milliseconds of additional delay for each roundtrip towards the WSJ (and ad network) servers, this doubled to six seconds.

Due to the speed of light in fiber, which is about two-thirds of that in a vacuum, every 60 miles of distance adds a millisecond to the round trip time. So a coast-to-coast trip is 50ms and intercontinental sea cables add significantly more delay. But the doubling in load time is a strange result; all modern browsers open several connections to a server and load several elements in parallel. This way, the round trip delays happen in parallel rather than sequentially.

The problem is that JavaScript has the ability to call the document.write() function to spit out additional HTML for inclusion in the rendered page. So the standard behavior of current browsers is to load and execute any external JavaScript they encounter in an HTML page before continuing to parse the rest of the page. Since external JavaScripts are nowadays heavily used for serving up ads (and also often for plenty of other functions), this can remove much of the parallelism from the page loading process. This means the browser is spending most of its time waiting for the network to deliver reply packets before it can continue loading more elements and rendering the page.

Web designers can decrease the load times of their pages by reducing the number of external JavaScripts, either by combining several pieces of JavaScript code into a single file or by embedding the code into the page's HTML. However, this doesn't really work for loading ads from external servers. For users who aren't quite prepared to experience a JavaScriptless web, Safari will be coming to the rescue with a new optimization in a future version, which is already available in the nightly WebKit builds.

The optimization is to parse the HTML to find elements that can be loaded in parallel with scripts rather than waiting for these elements to be encountered during the regular page building process. With this feature enabled, the WSJ page loads almost as fast with 50 ms extra delay as it does without the extra delay and without the optimization.

You must to comment.

Channel Ars Technica