4

How to calculate the page load time programmatically for a angular JS (single page application)? I have used window.performance.timing for multi-page application, but it's not works properly for single page application. anybody help for track page load time on single page application? Please help.

Jahir CU
  • 111
  • 2
  • 13

1 Answers1

4

I think you can achieve that by using Batarang + Chrome DevTools. It will help you track your app performance.

Update since the OP wanted to get page loading time programatically:

A possible solution might be the one provided to a similar question here.

You just have to get the current Date before loading any other scripts and get the date once the angular app hits the run phase. Then just subtract both dates and you get the loading time.

So, you will have:

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

And then in your main angular module:

angular.module('app', []).
    config(function() {
        // some code
    }).
    run(function($window) {
        console.log("Time until reaching run phase: ", Date.now() - $window.timerStart);
    });

Didn't test but it should work.

Community
  • 1
  • 1
pgrodrigues
  • 2,083
  • 1
  • 24
  • 28
  • I need to implement that on programmatically :(. – Jahir CU May 02 '16 at 10:19
  • 1
    Thanx for reply. i put these on my location change success function. var pageLoadTime = Date.now() - $window.performance.timing.navigationStart; may i believe this value as correct load time while location changes?. – Jahir CU May 02 '16 at 12:01
  • Because I got these value before ajax request completes. – Jahir CU May 02 '16 at 12:14
  • 1
    It really depends on what you are trying to measure. If you want to get the time spent during a route change you can listen to `$locationChangeStart` and `$locationChangeSuccess` events and compare the two dates. – pgrodrigues May 03 '16 at 08:21
  • I also found out this [github repo](https://github.com/mendhak/angular-performance) that may be useful to you for further reading. – pgrodrigues May 03 '16 at 08:22