现代浏览器是并行还是按顺序加载脚本? [英] Are Modern browsers loading scripts parallel or sequentially?

查看:93
本文介绍了现代浏览器是并行还是按顺序加载脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估用于脚本加载优化的现有资源,但我在一些文章中提到过,例如



以下是一些关于脚本加载的文章:




I'm evaluating existing resources for script loading optimization, but I readed in some articles like this, refers to the older browsers block other downloads until this sequential script loading phase is completed. I check Modernizr(yepnope.js), headjs and ControlJs as candidates. But, is it necesary use this tools for parallel script loading in modern browsers?

解决方案

I believe by default most browsers today will actually load the scripts in parallel; but the browser will not by default execute the scripts in parallel. For example, in the following code the scripts will be loaded in parallel. In the image we can see that Fast1.js and Fast2.js loads extremely fast, but based on the time in the browser console, Fast2.js executes 3 seconds after Fast1.js executes.

Also, something else to keep in mind is that the order of the files can make a difference. The Backbone.js file depends on the underscore.js file. If we changed the order of these files, where bacbone.js is before underscore.js, an error will be raised.

<html >
<head>
    <title></title>
    <script src="scripts/fast1.js" type="text/javascript"></script>
    <script src="scripts/libs/jquery-1.8.3.js" type="text/javascript"></script>
    <script src="scripts/libs/underscore.js" type="text/javascript"></script>
    <script src="scripts/libs/backbone.js" type="text/javascript"></script>
    <script src="scripts/fast2.js" type="text/javascript"></script>
</head>
<body>
    Hello
    <script type="text/javascript">
        console.log("html:   " + Date());
    </script>
    <img src="imgs/bImg.png" />
</body>
</html>

Here's the code for the JavaScript file Fast1.js and Fast2.js

console.log("Fast 1: " + Date())

For Script loading I use Require.js. It also provides the benefit of organizing your code into modules that are in individual files.

Here's an a blog post I create on Browser Script Loading: Browser Script Loading

Here are a few articles on Script loading:

这篇关于现代浏览器是并行还是按顺序加载脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆