有没有比setTimeout和requestAnimationFrame更快的东西? [英] Is there anything faster than setTimeout and requestAnimationFrame?

查看:126
本文介绍了有没有比setTimeout和requestAnimationFrame更快的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我在浏览器上需要一个process.nextTick等价物。)

(I need a process.nextTick equivalent on browser.)

我正在努力充分利用javascript性能,所以我制作了一个简单的计数器.. 。
在一秒钟内,我连续调用一个只向变量添加一个的函数。

I'm trying to get the most out of javascript performance so I made a simple counter ... In a second I make continuous calls to a function that just adds one to a variable.

代码:codepen.io/rafaelcastrocouto/pen/gDFxt

The code: codepen.io/rafaelcastrocouto/pen/gDFxt

我使用setTimeout获得了大约250,使用google chrome / win7中的requestAnimationFrame获得了70。
我知道requestAnimationFrame与屏幕刷新率一致,所以我们怎样才能让它更快?

I got about 250 with setTimeout and 70 with requestAnimationFrame in google chrome / win7. I know requestAnimationFrame goes with screen refresh rate so, how can we make this faster?

PS:我知道asm.js

PS: I'm aware of asm.js

推荐答案

嗯,有 setImmediate() 立即运行代码,即您所期望的得到 setTimeout(0)

区别在于 setTimeout(0) 实际上并不立即运行; setTimeout 被钳制到最小等待时间(4ms),这就是为什么你的测试程序中只有250的计数。 setImmediate()确实立即运行,因此您的计数器测试使用它会高出几个数量级。

The difference is that setTimeout(0) doesn't actually run immediately; setTimeout is "clamped" to a minimum wait time (4ms), which is why you're only getting a count of 250 in your test program. setImmediate() really does run immediately, so your counter test will be orders of magnitude higher using it.

但是您可能需要检查 setImmediate 的浏览器支持 - 它在所有浏览器中都不可用。 (你可以使用 setTimeout(0)作为回退当然,但是你回到了它所施加的最短等待时间。)

However you may want to check browser support for setImmediate -- it's not available yet in all browsers. (you can use setTimeout(0) as a fallback of course though, but then you're back to the minimum wait time it imposes).

postMessage() 也是一个选项,并且可以获得相同的结果,虽然它是一个更复杂的API,因为它的目的是为了做更多的事情而不仅仅是一个简单的调用循环。此外,还有其他需要考虑的因素(请参阅链接的MDN文章了解更多信息)。

postMessage() is also an option, and can achieve much the same results, although it's a more complex API as it's intended for more doing a lot more than just a simple call loop. Plus there are other considerations to think of when using it (see the linked MDN article for more).

MDN网站还提到 setImmediate 的polyfill库,它使用 postMessage 和其他技术将 setImmediate 添加到尚不支持它的浏览器中。

The MDN site also mentions a polyfill library for setImmediate which uses postMessage and other techniques to add setImmediate into browsers that don't support it yet.

使用 requestAnimationFrame( ),你的测试程序应该得到60,因为这是每秒的标准帧数。如果你得到的不止这些,那么你的程序可能会运行超过一秒钟。

With requestAnimationFrame(), you ought to get 60 for your test program, since that's the standard number of frames per second. If you're getting more than that, then your program is probably running for more than an exact second.

你的计数测试中你永远不会得到高分数它,因为它只会每秒触发60次(如果硬件刷新帧速率因某种原因而降低,则更少),但如果您的任务涉及对显示器的更新,那么这就是您所需要的,因此您可以使用 requestAnimationFrame()限制它被调用的次数,从而释放程序中其他任务的资源。

You'll never get a high figure in your count test using it, because it only fires 60 times a second (or fewer if the hardware refresh frame-rate is lower for some reason), but if your task involves an update to the display then that's all you need, so you can use requestAnimationFrame() to limit the number of times it's called, and thus free up resources for other tasks in your program.

这个这就是 requestAnimationFrame()存在的原因。如果你关心的只是让代码尽可能频繁地运行,那就不要使用 requestAnimationFrame();请改用 setTimeout setImmediate 。但这不一定是性能最好的东西,因为它会耗尽浏览器为其他任务所需的处理器能力。

This is why requestAnimationFrame() exists. If all you care about is getting your code to run as often as possible then don't use requestAnimationFrame(); use setTimeout or setImmediate instead. But that's not necessarily the best thing for performance, because it will eat up the processor power that the browser needs for other tasks.

最终,性能不仅仅是获取要运行最多次的东西;它是为了让用户体验尽可能顺畅。这通常意味着对你的通话循环施加限制。

Ultimately, performance isn't just about getting something to run the maximum number of times; it's about making the user experience as smooth as possible. And that often means imposing limits on your call loops.

这篇关于有没有比setTimeout和requestAnimationFrame更快的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆