Performance concerns with multiple timers?

(Previously posted on the HTML 5 Game Dev forum, but since there were no responses and that’s going away, figured I’d move it over here.)

I’m working on a game where I want to do some checks every minute, every five minutes, and then every some amount of seconds (1 and 5 seconds now, but that might change).

Right now I’m adding the following in the main game scene:

// Add a main timer that runs every second.
this.time.addEvent({
	delay: 1000,
	callback: this.doOneSecondActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that runs every five seconds.
this.time.addEvent({
	delay: 5000,
	callback: this.doFiveSecondActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that should run every minute.
this.time.addEvent({
	delay: 60000,
	callback: this.doOneMinuteActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that should run every five minutes.
this.time.addEvent({
	delay: 1000 * 60 * 5,
	callback: this.doFiveMinuteActivities,
	callbackScope: this,
	loop: true
});

I realize that I shouldn’t prematurely optimize, but I’m wondering if I’m going to end up refactoring this later, or if it’s relatively safe to setup multiple timers in Phaser 3.

(The other way I can think of handling this is to determine what my smallest time is and then manually keep track of the last time the various events were triggered, adding/subtracting time to determine if an interval has passed. Since the game loop can be paused, I do realize that I might still need to check against a timestamp for certain things.)

Does anyone have experience with multiple timed events in Phaser 3?

I think 4 timers should be no problem at all.

1 Like

I agree with samme. The only overhead a Timer adds is a method call which checks every frame if it’s expired - that’s cheaper than even a Game Object, so 4 shouldn’t cause any problems at all.

With only one timer in a custom class the performance is very bad in a very simple game

Hi,
Show us your timer, i use a lot of timer everywhere, without performance problems…but you must not use classic js timers

I have made some changes in my simple game to not use timer and now the performance is better but still is not good. I have a callback to test the key pressed and sometimes the game stop for a while.It is strange because I don’t move anything on the screen and only change some text object.

This is the callback for the keydown event:

teclaPulsada(event){
//event.code= “KeyA”, event.keyCode= 65, key=“a”, which=65, console.log(event);

    let visibles=0, acierto=false;        
    
    //Compruebo si ha acertado
    for(let n=0; n<this.letreros.length; n++){
        let l=this.letreros[n];
        if(l.visible){
            visibles++;
            if(l.letra==event.key){
                visibles--;
                l.visible=false;
                this.imgs[l.numero].visible=false;
                //this.ponMas1(l.x, l.y);
                acierto=true;
            }
        }
    }
    
    //Si ya ha escrito todas las letras de esta fase
    if(acierto){
        this.aciertos++;            
        this.txtAciertos.setText("Aciertos: " + this.aciertos);
        
        if(this.aciertos>= this.maxAciertos){
            this.enMarcha=false;
            this.input.keyboard.removeAllListeners("keydown");
            this.panel.mostrar("¡Muy bien!", "Has superado el reto de Hexamano y has conseguido 10 golosinas telepáticas que acabamos de enviarte.\n\nAcabas de entrar en la lista de campeones de Mundo Vedoque")
        
        }else if(visibles==0){
            let usadas=aleatorios(6, this.teclas.length-1);
            
            for(let n=0; n<this.letreros.length; n++){
                let l=this.letreros[n];
                this.imgs[n].visible=true;
                l.visible=true;
                l.setText(this.teclas[usadas[n]]);
                l.letra=this.teclas[usadas[n]];
            }
            this.tiempoFase -= 1;   
            this.tiempo=this.tiempoFase;
            this.txtReloj.setText(this.tiempo);
        }
        
    }else{    
        //Si ha fallado le quito un acierto
        if(this.aciertos>0)
            this.aciertos--;            
        this.txtAciertos.setText("Aciertos: " + this.aciertos);
        this.tiempo = (this.tiempo>2) ? this.tiempo-2 : 0;
        this.txtReloj.setText(this.tiempo);
    }
}

Is the game stopping on a particular key? If i remember well, some browsers extensions can cause troubles(evernote)