如何减少这段 javascript 代码的 cpu 使用率?

发布于 2024-12-11 00:31:41 字数 480 浏览 3 评论 0原文

现在我正在优化一些js代码。
有一个名为appendXYZ的函数,它与其他函数一起在循环中调用。
它看起来如下所示:

function OuterFunc (){
  for(...){// about 150 times
     ...
     appendXYZ();
     //other dependent functions
     ...
  }
}

现在我很确定appendXYZ会导致CPU使用率很高 - 它可以达到50%,
但如果我删除这个功能,CPU 使用率只有 1%。
当CPU使用率达到50%时,浏览器几乎被冻结,页面缺乏响应能力。
更重要的是,OuterFunc 每 20 秒执行一次,appendXYZ 来自第三方脚本代码,我无法修改它。
那么如何优化这段代码?

现在我正在尝试使用setTimeout,但我不知道它是否有效。

nowadays i am optimizing some js code.
there is a function named appendXYZ,and it is invoked in a loop with other functions.
it looks like as the following:

function OuterFunc (){
  for(...){// about 150 times
     ...
     appendXYZ();
     //other dependent functions
     ...
  }
}

and now i am pretty sure that appendXYZ cause high cpu usage - it can reach 50%,
but if i remove this function,cpu usage is only 1%.
when the cpu usage is 50%,the browser is nearly frozen and the page is lack of responsiveness.
what is more ,the OuterFunc execute every 20 seconds and appendXYZ is from a third party script code and i cant modify it.
so how to optimize this code?

now i am trying to use setTimeout but i dont know whether it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大姐,你呐 2024-12-18 00:31:41

我不知道该函数的作用,但您可以尝试使其调用异步。

它可能工作也可能不工作,并且仍然需要相同数量的 CPU,但它至少应该释放浏览器一点。

function OuterFunc (){
  for( var i = 0; i < 150; i++ ){
     // ...
     setTimeout( appendXYZ, 0 );
     //other dependent functions
     // ...
  }
}

同样,这可能会破坏该功能。如果没有看到更多代码就无法判断。

如果你要传递参数,那么你需要类似的东西:

function invoker( j ) {
   return function() {
       appendXYZ( j );
   };
}

function OuterFunc (){
  for( var i = 0; i < 150; i++ ){
     // ...
     setTimeout( invoker( i ), 0 );
     //other dependent functions
     // ...
  }
}

I don't know what that function does, but you could try making its invocation asynchronous.

It may or may not work, and it will still require the same amount of CPU, but it should at least free up the browser a bit.

function OuterFunc (){
  for( var i = 0; i < 150; i++ ){
     // ...
     setTimeout( appendXYZ, 0 );
     //other dependent functions
     // ...
  }
}

Again this may break the function. Can't tell without seeing more code.

If you're passing arguments, then you'd need something like:

function invoker( j ) {
   return function() {
       appendXYZ( j );
   };
}

function OuterFunc (){
  for( var i = 0; i < 150; i++ ){
     // ...
     setTimeout( invoker( i ), 0 );
     //other dependent functions
     // ...
  }
}
无法回应 2024-12-18 00:31:41

如果您无法优化实际代码,则可以分散执行循环迭代以保持浏览器响应。根据 Robert Miller 的论文,您可以持有的最长时间建立一个 UI 并且仍然感觉对用户的响应时间是 100 毫秒。有关如何使用 setTimeout 执行此操作的技术,请参阅UI 响应能力和 javascript。

If there's nothing you can do to optimize the actual code, you can spread around the execution of the loop iterations to keep the browser responsive. According to Robert Miller's paper, the maximum amount of time you can hold up a UI and still have it feel responsive to the user is 100 milliseconds. For a technique of how to do this using setTimeout see UI responsiveness and javascript.

她如夕阳 2024-12-18 00:31:41

一种可能性是 OuterFunc 执行时间大于其重复间隔。
换句话说,OutherFunc 的执行时间超过 20 毫秒,并且每 20 秒调用一次,它将产生一个 stackoverflow 异常,因为该函数在无限循环中完成执行之前被调用。
如果您使用 setInterval 每 20 毫秒执行一次 OuterFunc 函数,则可以通过使用 setTimeout 调用来模拟 setInterval 来解决此问题 函数:

(function helper(){
    OutherFunc();
    // after the OutherFunc is done executing, trigger it after 20 milliseconds
    setTimeout(helper, 20);
})();

仅当 setInterval 是导致浏览器冻结的原因时,这可能会对您有所帮助。
如果这对您没有帮助,并且您不太关心旧浏览器,也许您可​​以使用 网络工作者。这样你的代码就会在不同的线程中执行,这肯定会加快你的应用程序的速度(又名再见浏览器冻结)。

希望这有帮助!

A possibility is that the OuterFunc execution time is bigger that it's repetition interval.
In other words, the OutherFunc takes longer than 20 milliseconds to execute and being called every 20 seconds it will produce a stackoverflow exception because the function is being called before it finished it's execution in an infinite loop.
If you are using setInterval to execute the OuterFunc function every 20 milliseconds, this can be fixed by using setTimeout calls to simulate the setInterval function :

(function helper(){
    OutherFunc();
    // after the OutherFunc is done executing, trigger it after 20 milliseconds
    setTimeout(helper, 20);
})();

This might help you only if the setInterval is the cause of the browser freeze.
If this doesn't help you and if you don't care that much about old browsers, maybe you could implement a sort of "threading" using web-workers. This way your code gets executed in different threads which will definitely speed up your app (a.k.a bye bye browser freeze).

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文