D
D
dmppka2014-03-30 09:22:36
JavaScript
dmppka, 2014-03-30 09:22:36

JavaScript - many timers VS one global timer, which is better?

Hello. I ask for advice.
Let's say I have an array of objects with 300 elements. Each object has a current position and speed, i.e. objects are moving.
With requestAnimationFrame() , I want to display these objects on the canvas. Since the objects are moving, I need to change the coordinates of the object every N milliseconds (where N - depends on the speed of the object).
The first idea is to hang a timer for each object, which will change its coordinates.
But then, it turns out that I will have 300 timers, each of which will perform some calculations every N milliseconds, moreover, the values ​​of N are rather small, 5-30 ms.
After some thought, I came up with an idea. Or maybe make a global timer that will run the method, say, once every 5 milliseconds and already in this method take each object and calculate the current coordinates for it depending on the speed?
How big will the performance difference be? Which of the methods justifies itself? Or maybe there is another solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vampiro, 2014-03-30
@Vampiro

The timer will lag behind, usually they hang one event for the process of "drawing" the screen, in which they calculate the delta in time since the last drawing and recalculate the coordinates if necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question