I used to get page load time in my react project by window.performance.timing but I found that This property is deprecated. so I tried to get load time by PerformanceNavigationTiming.
This is the code:
window.addEventListener("load", function() {
const page_load_time = performance.getEntriesByType("navigation")[0];
console.log(page_load_time);
console.log(`${page_load_time.loadEventEnd}`);
});
console.log(page_load_time); give me an object that contains loadEventEnd
number in ms but console.log(${page_load_time.loadEventEnd}); gives me zero
- Why zero?
- Is this the right way to get page load time?
TSX Way:
JSX Way:
Make sure to wrap this in a
setTimeout(() => {}, 0)so theonLoadevent completes first and gives accurate timing