Skip to content

Instantly share code, notes, and snippets.

@idanen
Last active November 20, 2016 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idanen/317fe6cc12df4287121569a4abb11588 to your computer and use it in GitHub Desktop.
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)
(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