Last active
November 20, 2016 11:35
-
-
Save idanen/317fe6cc12df4287121569a4abb11588 to your computer and use it in GitHub Desktop.
Measure load time of an element in an AngularJS app (with UI-Router)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var nameOfState = 'name of the state we want to start the measuring from', | |
timingTag = 'an arbitrary tag to tag the timestamps', | |
elementSelector = `a selector of the element we're wating for`, | |
observer; | |
angular.element(document).injector().get('$rootScope').$on('$stateChangeSuccess', (e, toState) => { | |
if (toState.name === nameOfState) { | |
console.time(timingTag); | |
observer = new MutationObserver(e => { | |
if (document.querySelector(elementSelector)) { | |
console.timeEnd(timingTag); | |
observer.disconnect(); | |
} | |
}); | |
observer.observe(document.body, {childList: true, attributes: false, subtree: true}); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment