有没有什么办法可以让自己变得“好”呢?我的 JavaScript 执行?
我想在浏览器窗口中运行一些计算,但我不希望它减慢客户端计算机的用户交互速度,特别是对于单核计算机。有没有什么方法可以调整我执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了延迟执行你的计算之外,我想不出别的办法。例如,将所有工作分成小块,然后按顺序运行它们,并在每个任务之间有一些延迟(使用
setTimeout
或setInterval
)。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
orsetInterval
) between each task.创建开环... 示例
这是一个闭环
这是一个开环
您可以在开环内进行更多或更少的工作,但这是总体思路。我已经多次针对类似问题进行了此操作,并且浏览器工作非常顺利。
Create open loops... an example
This is a close loop
This is an open loop
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.
为了提高速度,尝试使用单个多维数组来包含数据,然后在函数末尾使用单个联接将该数组转换为字符串以进行输出,并使用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.
我做了一些测试,浏览器在突发工作之间需要相当长的时间才能做出合理的响应:
I did some testing, and the browser needs quite some time between bursts of work to be reasonably responsive: