Jump to content

[Performance] SetTimeout vs Main loop queue


cruzlutor
 Share

Recommended Posts

And loop is presumably tied to requestAnimationFrame (raf) or setTimeout? If it is tied to raf and you're supporting older browsers then you'll probably have a fallback in there to use setTimeout (or even setInterval, but thats a far worse solution).

The simple answer is, no performance difference.

It is likely that adding it to main loop is slight less performant as it has to do a conditional, but this is so ridiculously cheap as to make no matter.

The question for you really is not one of performance, but what is easier for you (as dev) to manage.

If you are calling functions that aren't really part of the lifecycle of your app/game, such as delaying an ajax request (for example), then setTimeout probably makes more sense, but if you're calling functions that need to be called regularly, maybe the main loop makes more sense. By shoving them all in the main loop they are all in one place, which from a management point of view could be good, or, it might get two noisy in there, so it could be bad.

I guess the point is that there is no hard-and-fast rule and you have to weigh up the pros and cons for your situation and choose appropriately, safe in the knowledge that you can probably change this sort of thing fairly easier further down the line (something to watch out for is which functions you are calling i.e. are they available everywhere to call? is the data they need to work with also available? these are architectural issues and could have issues if you want to change later).

Link to comment
Share on other sites

Just now, mattstyles said:

If you are calling functions that aren't really part of the lifecycle of your app/game, such as delaying an ajax request (for example), then setTimeout probably makes more sense, but if you're calling functions that need to be called regularly, maybe the main loop makes more sense. By shoving them all in the main loop they are all in one place, which from a management point of view could be good, or, it might get two noisy in there, so it could be bad.

That is a good point : ), and the main loop solution is easier to manage for me.

thanks 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...