Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 522
Robert Hurst Avatar asked Apr 27 '10 05:04

Robert Hurst


People also ask

Which is more efficient for loop or while loop in JavaScript?

Result. I have perform around 10 test and got conclude that while loop is faster then for loop if we are comparing very high iteration execution. for smaller iteration up to 10⁴ both perform nearly same. We observe that while loop take almost double time then for loop.

Is find faster than for loop JavaScript?

It is fairly obvious that the native find() function is faster than a loop algorithm.

Which loop is faster in jQuery?

jQuery each() vs for loopThe 'FOR' loop being a native Javascript function is undoubtedly faster, however the scope is all the same, so in some cases the . each() method would be preferable, as each iteration is its own scope.

What is while true in JavaScript?

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.


1 Answers

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

like image 121
Amber Avatar answered Sep 19 '22 03:09

Amber