Reputation: 9092
Just wondering if its worth it to make a monolithic loop function or just add loops were they're needed.
The big loop option would just be a loop of callbacks that are added dynamically with an add function.
adding a function would look like this
setLoop(function(){
alert('hahaha! I\'m a really annoying loop that bugs you every tenth of a second');
});
setLoop would add the function to the monolithic loop.
so is the is worth anything in performance or should I just stick to lots of little loops using setInterval?
heres the game
http://thinktankdesign.ca/metropolis/
and all of its libraries
http://thinktankdesign.ca/metropolis/scripts/base.js
http://thinktankdesign.ca/metropolis/scripts/menu.js
http://thinktankdesign.ca/metropolis/scripts/grid.js
http://thinktankdesign.ca/metropolis/scripts/cursor.js
http://thinktankdesign.ca/metropolis/scripts/game_logic/controls.js
http://thinktankdesign.ca/metropolis/scripts/game_logic/automata.js
if I stick to individual loops there will be thousands of them because of the number of animation loops.
The game is a tower builder and complicated things like elevators and cars/peds. Not to mention loops for the automata, controlling events such as VIPs and fires and such. When functional (a year or two) it will be a lot like Sim Tower but iso-metric instead of a side scroller.
Upvotes: 5
Views: 773
Reputation:
Your question is impossible to answer correctly without sufficient details:
Without sufficient details every answer is correct, thereby rendering your question pointless.
Upvotes: 0
Reputation: 526723
Having them all scheduled separately means that things can happen (like processing user interaction events, et cetera) in between some of the sub-tasks occurring and others. With one big monolithic loop, each cycle of the loop will execute everything on that cycle before returning control back to user input. Thus, if you're truly doing a lot of things it's probably better to schedule them independently to allow the browser to be "smarter" about what to do when.
The exception would be if you actually intend/require that all of the things happening in the same "cycle" occur without offset from one another, in which case the "big loop" approach would be required to guarantee they all trigger at the exact same time (or at least, as close together as execution time permits).
Upvotes: 3
Reputation: 63126
I would imagine that you can just use setInterval, keeping the individual loops with control over each one is going to give you better flexibility, unless EVERY single item needs to be queued, but even at that point I don't see where you would get a performance improvement from this that would matter.
Upvotes: 2