<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ramesh Murugesan</title>
    <description>The latest articles on DEV Community by Ramesh Murugesan (@macroramesh6).</description>
    <link>https://dev.to/macroramesh6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F467202%2Fd9a6abf1-e855-408a-8d84-d3d51d80f2f4.jpeg</url>
      <title>DEV Community: Ramesh Murugesan</title>
      <link>https://dev.to/macroramesh6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/macroramesh6"/>
    <language>en</language>
    <item>
      <title>Are you facing high CPU usage on animation? requestAnimationFrame</title>
      <dc:creator>Ramesh Murugesan</dc:creator>
      <pubDate>Wed, 12 May 2021 14:06:35 +0000</pubDate>
      <link>https://dev.to/macroramesh6/are-you-facing-high-cpu-usage-on-animation-requestanimationframe-3c94</link>
      <guid>https://dev.to/macroramesh6/are-you-facing-high-cpu-usage-on-animation-requestanimationframe-3c94</guid>
      <description>&lt;h4&gt;
  
  
  Do you want smooth JavaScript animation?
&lt;/h4&gt;

&lt;p&gt;Creating animation in the JavaScript is simple. Most devs likely have used either &lt;code&gt;setInterval()&lt;/code&gt; or &lt;code&gt;setTimeout()&lt;/code&gt; functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
    // Drawing code goes here
}
setInterval(draw, 100);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
    setTimeout(draw, 100);
    // Drawing code goes here
}
draw();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are actually a great choice. It will execute every millisecond. You can perform a repainting on each execution. &lt;/p&gt;

&lt;h5&gt;
  
  
  Hmm… How about if you have a monitor with a 60Hz refresh rate, and you want to achieve 60 FPS?
&lt;/h5&gt;

&lt;p&gt;You have about 16.7 milliseconds (1000 / 60) to execute your animation code to render each frame.&lt;/p&gt;

&lt;p&gt;It's a good idea to perform the event with an FPS calculation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const frameDuration = 1/60 * 1000; // 60 fps
const slideSomeMore = function( moved ) {
    let next = window.performance.now() + frameDuration;
    while ( '&amp;lt;Animation Condiontion&amp;gt;' ) {
        if ( window.performance.now() &amp;lt; next ) {
            continue;
        }

        // Drawing code goes here

        setTimeout( () =&amp;gt; slideSomeMore( moved ), 0 );
        return;
    }
}
slideSomeMore( 0 );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logically, the above code is perfectly fine. You've done these millions of time before.&lt;/p&gt;

&lt;p&gt;Okay, let's see the performance of CPU and Memory usage in the screenshot. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26jvis97ansboi6zdem0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26jvis97ansboi6zdem0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's quite high, right? &lt;/p&gt;

&lt;p&gt;Okay, let's see the timeline &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7v5golysbq62puqn28g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7v5golysbq62puqn28g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;setTimeout()&lt;/code&gt; takes a long time to complete the recursive function. It happened until the animation gets completed.&lt;/p&gt;

&lt;p&gt;Another problem with this approach is, if you see the mid of the animation, the previous cycle is being busy, the current cycle is added up in the execution and the next cycle is going to be delayed!&lt;/p&gt;

&lt;p&gt;Yes, you will be ended up having poor performance in delayed/slow animation along with high CPU and memory usage. &lt;/p&gt;

&lt;h4&gt;
  
  
  What's the solution??
&lt;/h4&gt;

&lt;p&gt;requestAnimationFrame is the Saviour&lt;/p&gt;

&lt;p&gt;&lt;code&gt;requestAnimationFrame()&lt;/code&gt; is a specialized looping function created for running animations efficiently instead of event loop (setTimeout() or setInterval()) in the browser. And the result is smoother and more accurate.&lt;/p&gt;

&lt;p&gt;It is a relatively recent browser API which is a specialized enqueueing function created for running animations efficiently in the browser.&lt;/p&gt;

&lt;p&gt;requestAnimationFrame() always tries to get as close to this magic 60 FPS value as possible. Sometimes, it isn't possible if you have a really complex animation, and if you are running it on a slow computer, your frame rate will be less. In all cases, requestAnimationFrame() will always do the best it can with what it has available.&lt;/p&gt;

&lt;p&gt;It is very CPU-friendly, causing animations to stop if the current window or tab is not visible. And battery savings, browsers also implemented throttling for those events, allowing max 1 execution per second.&lt;/p&gt;

&lt;p&gt;Using requestAnimationFrame the browser can further optimize the resource consumption and make the animations smoother.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sounds great! Are there any cons using requestAnimationFrame()?
&lt;/h4&gt;

&lt;p&gt;Well, not exactly. Because this is a new API Microsoft supports &lt;code&gt;requestAnimationFrame&lt;/code&gt; from version 10 of Internet Explorer. Here are the supported browsers to use this API function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0cni2c6nqq6k2pd4lxs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0cni2c6nqq6k2pd4lxs.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you want to know more about requestAnimationFrame? Here Lilli Thompson in Google I/O explaining the requestAnimationFrame in detail. Start the video at 48:04 &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XAqIpGU8ZZk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
