简体   繁体   English

如何测量大型阵列的加载时间?

[英]how do I measure the load time of a large array?

I have one rather large array in a javascript widget I'm writing and I want to find out if it'd be more efficient in terms of browser resources to leave it as just one array or break it into several smaller arrays? 我正在写一个javascript小部件中有一个相当大的数组,我想知道它是否在浏览器资源方面更有效率,只将其作为一个数组或将其分解为几个较小的数组? Can anyone recommend a method of testing the differences? 任何人都可以推荐一种测试差异的方法吗?

Could use firefox or chrome to do a console.log(new Date.toString()) before and after the different methods you are trying to compare. 可以使用firefox或chrome在您尝试比较的不同方法之前和之后执行console.log(new Date.toString())。 I don't think breaking up the routine wil reduce time. 我不认为打破常规会减少时间。

you can always use console.time('timerName') and console.timeEnd('timerName') to set a timer and find out elapsed time between two points of your javascript code, then compare the results: 你总是可以使用console.time('timerName')console.timeEnd('timerName')设置一个计时器,找出javascript代码两点之间的经过时间,然后比较结果:

console.time('BigArray');
var arr1 = [];
for(var i=0; i<200000; i++){
    arr1.push('test');
}
console.timeEnd('BigArray');


console.time('SeveralArrays');
for(var i=0; i<200000; i++){
    var arr2 = ['test'];
}
console.timeEnd('SeveralArrays');

the output will be something like: 输出将是这样的:

BigArray: 123ms
SeveralArrays: 456ms

Just splitting the arrays won't improve processing speed. 仅拆分数组不会提高处理速度。

I suggest using Web Workers. 我建议使用Web Workers。 They will offload the task to a separate thread, which could speed things up if you split the task into chunks. 他们会将任务卸载到一个单独的线程,如果将任务拆分成块,可以加快速度。 It's totally dependent on how many cores are available, though. 但这完全取决于可用的核心数量。

It will also prevent the UI from hanging if you're processing really huge sets of data. 如果您正在处理非常庞大的数据集,它还会阻止UI挂起。 Very useful from a UX perspective. 从UX的角度来看非常有用。

I have used Paralell.js before, and I like it. 我之前使用过Paralell.js,我喜欢它。 It provides a fallback for browsers that don't support Web Workers. 它为不支持Web Workers的浏览器提供了后备。

http://adambom.github.io/parallel.js/ http://adambom.github.io/parallel.js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM