What is better for the overall performance and feel of the game: one setInterval performing all the work, or many of them doing individual tasks?

Posted by Bane on Game Development See other posts from Game Development or by Bane
Published on 2012-04-06T22:13:14Z Indexed on 2012/04/06 23:40 UTC
Read the original article Hit count: 217

Filed under:
|
|

This question is, I suppose, not limited to Javascript, but it is the language I use to create my game, so I'll use it as an example.

For now, I have structured my HTML5 game like this:

var fps = 60;
var game = new Game();

setInterval(game.update, 1000/fps);

And game.update looks like this:

this.update = function()
{
    this.parseInput();
    this.logic();
    this.physics();
    this.draw();
}

This seems a bit inefficient, maybe I don't need to do all of those things at once. An obvious alternative would be to have more intervals performing individual tasks, but is it worth it?

var fps = 60;
var game = new Game();

setInterval(game.draw, 1000/fps);
setInterval(game.physics, 1000/a); //where "a" is some constant, performing the same function as "fps"
...

With which approach should I go and why? Is there a better alternative? Also, in case the second approach is the best, how frequently should I perform the tasks?

© Game Development or respective owner

Related posts about JavaScript

Related posts about html5