Man*_*tre 4 console dependencies requirejs
是否有任何技巧可以记录控制台中require.js加载的模块?例如,加载jQuery加载下划线加载Backbone
我需要这个来了解下载每个模块并记录相同的时间以在不同的环境下测试它需要多少时间.
谢谢,Mandar Katre
您可以尝试类似于这个小提琴的东西,并使用内部API onResourceLoad.这不会给出完全准确的加载时间,但它可以让您了解已经请求了哪些模块,以及在给定的开始时间完成加载后多长时间.
<script>
require = {
paths: {
"jquery": "http://code.jquery.com/jquery-2.0.3",
"underscore": "http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min"
},
shim: {
"underscore": {
deps: ["jquery"],
exports: "_"
}
}
};
</script>
<script src="http://requirejs.org/docs/release/2.1.8/comments/require.js"></script>
<script>
// https://github.com/jrburke/requirejs/wiki/Internal-API:-onResourceLoad
requirejs.onResourceLoad = function(context, map, depArray) {
var duration = new Date() - start;
console.log("onResourceLoad", duration + "ms", map.id);
}
</script>
Run Code Online (Sandbox Code Playgroud)
而这个JS
start = +new Date();
require(["jquery", "underscore"], function() {
// log the global context's defineds
console.log("require.s.contexts._.defined", require.s.contexts._.defined);
});
Run Code Online (Sandbox Code Playgroud)
在测试中生成此输出:
onResourceLoad 140ms jquery
onResourceLoad 167ms underscore
require.s.contexts._.defined Object {jquery: function, underscore: function}
Run Code Online (Sandbox Code Playgroud)