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

165
Visualizações
How to get page load duration using JavaScript

I am trying to get the time it takes to load a page and I am not sure of the correct way to run this script.

I thought I could do this:

window.addEventListener("load", function(){
    console.log(performance.getEntriesByType("navigation")[0].duration);
});

However it always returns 0;

If I run it in the console after, it works fine. Is there another event I can use that runs after the window load event? What is the correct usage of this function?

EDIT: It seems using setTimeout adds it as the first thing that runs after the page has completely loaded. Working code is:

window.addEventListener("load", function(){    
    setTimeout(function(){
        console.log(performance.getEntriesByType("navigation")[0].duration);
    }, 0);
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can get load time using the performance API given by your browser

 let loadTime = window.performace.timing.loadEventEnd - window.performance.timing.navigationStart;

Checkout https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API for more

about 4 years ago · Juan Pablo Isaza Relatório

0

You can also poll for the value being set:

const perf = () => {
  const duration = performance.getEntriesByType("navigation")[0].duration;
  if (!duration) setTimeout(perf, 0);
  else console.log({ duration });
}
window.addEventListener('DOMContentLoaded', perf);
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