Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Angular setTimeout Poor Performance

I have a component that I need to re-render every time with different @input variables because I have some logic implemented in ngOnInit(). I am doing this by using following method in parent component with showSettings being used to hide and then show the component (with new inputs) after 100ms delay.

private _reloadSettings() {
    this.showSettings = false;
    let a = performance.now();
    setTimeout(() => {
         this.showSettings = true;
         console.log(performance.now() - a);
     }, 100);
    }

Everything is working fine except that the re-render takes so much time than expected i.e. it should be around 100ms (or a little over) but actually I am getting like 8000ms to 10000ms, and with each re-render it gets worst.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In other parts of your application, you are doing too much work.

We cannot solve your problem in this case. I would go about looking into angular change detection.

Other than that, I will show you few things to look for in your codebase:

ChangeDetection

You could switch from ChangeDetection.Default to ChangeDetection.OnPush. This may break parts of your app, but if you would fix those, you will get a big performance boost for all app-wide actions as setTimeout is.

In the example above, you are listening to service observable, it might be anything

Leaking subscription

constructor(myService) {
  myService.data.subscribe(data => {
    this.array = data.array;
  });
}

The code above is bad, the subscription is never removed, meaning the browser cannot remove the component from memory AND the code inside subscribe method runs every time. It is fine in this case, but if you were to do some computation inside, it could get bad quickly.

You can fix it by:

  1. Subscribing to first emission only.
myService.data.pipe(
  take(1),
).subscribe(data => {
  this.array = data.array;
});
  1. Replacing it with piped observable. This will keep it reactive, but the angular will
this.array$ = myService.data.pipe(
  map(data => data.array),
  // Uncomment next line, if the observable is used multiple time
  // shareReply({ ... }), // 
)
about 4 years ago · Juan Pablo Isaza Relatório

0

Try using queueMicrotask instead of setTimeOut.

queueMicrotask(() => {
     this.showSettings = true;
     console.log(performance.now() - a);
 });

SetTimeout is a macro task. The call back function provided in setTimeout will be loaded into the call back queue(aka MacroTaskQueue). It waits until the call stack is empty.

The micro task also has similar behavior. But, the microtask gets higher priority, and hence it runs prior to the macro task queue.

read more about it here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda