有没有什么办法可以让自己变得“好”呢?我的 JavaScript 执行?

发布于 2024-08-30 10:33:46 字数 112 浏览 4 评论 0原文

我想在浏览器窗口中运行一些计算,但我不希望它减慢客户端计算机的用户交互速度,特别是对于单核计算机。有没有什么方法可以调整我执行 JavaScript 的良好级别,以便它能够尽可能快地执行而不降低机器的响应能力?

I'd like to run some calculations in a browser window, but I don't want it to slow the client computer down for user interaction, especially for single core machines. Is there some way to adjust the nice level of my executing JavaScript so that it will execute as fast as possible without detracting from the responsiveness of the machine?

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

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

发布评论

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

评论(5

梦初启 2024-09-06 10:33:46

除了延迟执行你的计算之外,我想不出别的办法。例如,将所有工作分成小块,然后按顺序运行它们,并在每个任务之间有一些延迟(使用 setTimeoutsetInterval)。

I can't think of anything else than delaying execution of your calculations. In example, dividing all work into small pieces and then running them sequentially with some delay (using setTimeout or setInterval) between each task.

我很坚强 2024-09-06 10:33:46

创建开环... 示例

这是一个闭环

for( i = 0 ; i < 10000 ; i++)
{
    doSomeMath(i);
}

这是一个开环

i = 0;
iMax = 10000
var MyWorkingThread =
setInterval(function()
{
    if( i < iMax)
    {
       doSomeMath(i);           
       i++;
    }
    else
    {
        clearInterval(MyWorkingThread);
    }

},1);

您可以在开环内进行更多或更少的工作,但这是总体思路。我已经多次针对类似问题进行了此操作,并且浏览器工作非常顺利。

Create open loops... an example

This is a close loop

for( i = 0 ; i < 10000 ; i++)
{
    doSomeMath(i);
}

This is an open loop

i = 0;
iMax = 10000
var MyWorkingThread =
setInterval(function()
{
    if( i < iMax)
    {
       doSomeMath(i);           
       i++;
    }
    else
    {
        clearInterval(MyWorkingThread);
    }

},1);

You can make a more, or a less work inside the open loop, but this is the general idea. I have made this many times for similar issues and I left the browser work very smooth.

葬心 2024-09-06 10:33:46

为了提高速度,尝试使用单个多维数组来包含数据,然后在函数末尾使用单个联接将该数组转换为字符串以进行输出,并使用innerHTML 方法输出该数据。这是在 JavaScript 中包含和提供数据的最快方法。绝对不要使用 DOM 方法或元素来输出数据,因为那样会慢 4 倍。

尽可能少地输出数据。这将取决于您使用哪个事件来执行函数。我建议不要使用 onload 事件,因为这会减慢页面的初始加载时间。我建议使用与按钮关联的 onclick 函数,因为这样用户就会意识到他们导致执行速度减慢了页面的速度。

For speed attempt to use a single multidimensional array to contain your data and then at the end of your function use a single join to convert that array to a string for output and output that data using the innerHTML method. That is the fastest possible method of containing and serving data in JavaScript. Definitely do not use DOM methods or elements to output your data as that is about 4 times as slow.

Output your data as few times as possible. This is going to be determined upon which event you use to execute your function. I recommend not using the onload event as this will slow the initial load time of your page. I would recommend using the onclick function associated with a button, because then the user is aware that they caused the execution that is slowing down your page.

君勿笑 2024-09-06 10:33:46

我做了一些测试,浏览器在突发工作之间需要相当长的时间才能做出合理的响应:

function work(cnt) {
  // do some heavy work
  for (var i=0;i<100000000;i++) ;
  // start next work
  if (cnt > 0) {
    window.setTimeout(function(){work(cnt-1);},200);
  }
}

I did some testing, and the browser needs quite some time between bursts of work to be reasonably responsive:

function work(cnt) {
  // do some heavy work
  for (var i=0;i<100000000;i++) ;
  // start next work
  if (cnt > 0) {
    window.setTimeout(function(){work(cnt-1);},200);
  }
}
╰つ倒转 2024-09-06 10:33:46
  1. 使用 ajax 请求在服务器上运行计算
  2. 打开一个新窗口或框架并在那里运行代码
  3. 在循环中运行代码分成间隔
  4. 准备好使用 web-worker 进程(html5 异步脚本)
  1. run the calculation on the server with an ajax request
  2. open a new window or frame and run the code there
  3. run the code in a loop broken into intervals
  4. be ready to use web-worker processes (html5 asynchronous script)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文