0

When I'm building web apps, I like to put a distinction between "important" code, and "superfluous". "Important" code being code that is required to run the app's features and is usually tied to a user interaction. "Superfluous" code is code that runs in the background and might handle such things as moving carousel images, checking for updates, or adding elements to a news feed.

When optimizing a web app, it seems like it would be a good idea to wrap any "superfluous" JS in a window.requestAnimationFrame() to prevent it from blocking the more important JS and slowing down user interactions. Nearly all documented uses for window.requestAnimationFrame() are related to animations, but I don't see why it couldn't be used in other contexts.

tl;dr: Are there any downsides to using window.requestAnimationFrame() outside of animation-related code?

elliottregan
  • 1,301
  • 1
  • 13
  • 33
  • 1
    What other use could `RAF` be used for? Does frames per second apply to anything else in JS? For background tasks, [web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) sounds like what you are thinking about. – zer00ne Jul 14 '16 at 18:44
  • I'm thinking for simpler bits of code tied more directly to DOM manipulation. For instance, changing a classes on a long list of elements, or swapping a bunch of images out. – elliottregan Jul 14 '16 at 18:54
  • Unless we are talking about thousands of nodes/elements at a time, I think good old `for` loops are ok. Don't really know until you try something. What have you tried and can you make a [MCVE (**M**inimal, **C**omplete, and **V**erifiable **E**xample)](http://stackoverflow.com/help/mcve)? – zer00ne Jul 14 '16 at 19:11

0 Answers0