简体   繁体   English

jsperf测试setInterval()与requestAnimationFrame()

[英]jsperf testing setInterval() vs requestAnimationFrame()

Can someone explain why turnEvenOld(250, 250) (0.089ms) runs much much more faster than turnEvent(250, 250) (0.447ms)? 有人可以解释为什么turnEvenOld(250, 250) (0.089ms)比turnEvent(250, 250) 250,250 turnEvenOld(250, 250) (0.447ms)运行快得多吗?

I thought using requestAnimationFrame() was a lot faster and cheaper to run than setInterval() ? 我以为使用requestAnimationFrame()setInterval()更快,更便宜地运行?

setInterval(): setInterval():

var turnEventOLD = function turnEvent(AnX, AnY) {
    ----VARIABLES----
    temp = setInterval(myAnimation1, 1000/60);
    function myAnimation1() {
        ----DRAWINGCANVAS------
        -----
        ----CONDITIONS--------
        if (one301 && one401) {
            clearInterval(temp);
        }
    }
}

requestAnimationFrame(): requestAnimationFrame():

var turnEvent = function turnEvent(AnX, AnY) {
   ----VARIABLES-----
    function render() {
        ----DRAWING CANVAS-----
        ------
        ----CONDITIONS---------
        if (one301 && one401) {
            ---stop requestAnimation--
        }
    }
    (function animloop(){
        ----CONDTION-----
        requestAnimationFrame(animloop);
        render();
    })();
}

RequestAnimation frame is not necessarily "faster" than setInterval. RequestAnimation帧不一定比setInterval“更快”。 It actually does something different. 它实际上做了一些不同的事情。

setInterval will wait a given number of milliseconds while requestAnimationFrame will wait until the page is ready to repaint. setInterval将等待给定的毫秒数,而requestAnimationFrame将等待直到页面准备重新绘制为止。 Depending on the time in your setInterval call, the time setInterval waits could be shorter or longer than the time until the next repaint. 根据setInterval调用中的时间,setInterval等待的时间可能比下一次重新绘制之前的时间短或长。

It is better to use requestAnimationFrame for animations so you're sure you change the visual elements before the next repaint instead of being potentially out of synch with the page repaints. 最好对动画使用requestAnimationFrame,以便确保在下一次重绘之前更改视觉元素,而不是与页面重绘不同步。

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

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