How to find page load time in angular?

2019-07-03 23:17发布

Is this the right way to find page load time in angular2+?? I don't think so, It's just the time difference between the route change.How can we add the page DOM content load time to it.??

this.router.events.subscribe((val) => {
    const navigationComplete = val instanceof NavigationEnd;
    const navigationStart = val instanceof NavigationStart;
    if(navigationStart){
        this.navigationStartTime = (new Date).getTime();
    }
    if (navigationComplete) {
        this.currentPage = this.router.url; // Current page route
        this.currentLocation = (this.platformLocation as any).location.href; // Current page url
        let endTime = (new Date).getTime();
        this.pageLoadTime = Number(endTime) - Number(this.navigationStartTime); // Page load time
    }
});

3条回答
Ridiculous、
2楼-- · 2019-07-03 23:56

you can definitely use chrome dev tools to analyse perfomance as mentioned by @AurA
separately in the above all scripts you can initialise

<script type="text/javascript">
    var timerStart = Date.now();
</script>

and in the main component

ngAfterViewInit(){
   console.log("Time until reaching run phase: ", Date.now() - $window.timerStart);
}

Havent tested. But should run.

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-03 23:58

You can use simple javascript code inside ng-init function

var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart; 
    console.log('Page load time is '+ loadTime);
查看更多
做个烂人
4楼-- · 2019-07-04 00:10

You can use NavigationStart and ngAfterViewInit to track exact load time.

查看更多
登录 后发表回答