Categories
JavaScript Answers

How to calculate page load time in JavaScript?

Spread the love

Sometimes, we want to calculate page load time in JavaScript.

In this article, we’ll look at how to calculate page load time in JavaScript.

How to calculate page load time in JavaScript?

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.

For instance, we write

const loadTime =
  window.performance.timing.domContentLoadedEventEnd -
  window.performance.timing.navigationStart;

window.performance.timing.domContentLoadedEventEnd is the timestamp when DOM finished loading.

And window.performance.timing.navigationStart is the timestamp when navigation started.

We subtract window.performance.timing.domContentLoadedEventEnd by window.performance.timing.navigationStart to get the page load time in milliseconds.

Conclusion

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.