cruzlutor

[Performance] SetTimeout vs Main loop queue

Recommended Posts

Hello guys

I'm working on a javascript game, some times I need to call a function 2, 3 or X seconds after an action, the question is.

What is better in performance, create multiple setTimeout() or use the main loop to evaluate a function queue?

 

thanks

Share this post


Link to post
Share on other sites

With time tick on main loop

init(){
  this.attackTimer = game.time
}

loop(){
  // attack 1 time each second
  if(game.time > this.attackTimer + 1000){
    this.attack()
    this.attackTimer = game.time
  }
}

But you can have a array of functions to evaluate

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
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 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.