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.