开发者

Whats faster in Javascript a bunch of small setInterval loops, or one big one?

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 j开发者_运维知识库ust 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.


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


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.


Your question is impossible to answer correctly without sufficient details:

  • How big is your monolithic loop?
  • How many small loops do you wish to add?
  • How small are those small loops?
  • What are you attempting to accomplish with your loops?
  • Will the small loops interact, such as altering the relationship of variables or the value of closures?
  • Would there be any expectation for loop redundancy to catch missing otherwise escaped by logic consideration?

Without sufficient details every answer is correct, thereby rendering your question pointless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜