Skip to content

Instantly share code, notes, and snippets.

@curlup
Forked from pmeenan/user-timing-rum.js
Last active August 29, 2015 14:20
Show Gist options
  • Save curlup/f760bee40b7b51ba9f24 to your computer and use it in GitHub Desktop.
Save curlup/f760bee40b7b51ba9f24 to your computer and use it in GitHub Desktop.
/*
Strategically place:
markUserTime('some event');
Through your code to get measurements for when various activities complete. It
will also generate timeline events so you can see them in Chrome's dev tools.
A good use case is to add them inline to sections of your site that are
above-the fold (right after the menu, right after the main story, etc) and also
in the onload handler for any critical above-the-fold images.
(coming soon) WebPagetest will report on any user timing events that were recorded
and also expose a high-level metric for when the last user timing event fired.
With any luck we can also get RUM solutions to start reporting user timing events
and we can significantly loosen our dependency on the (very broken) document
onload times.
*/
;(function () {
var object = typeof window != 'undefined' ? window : exports,
marks = [];
object.performance || (object.performance = {});
object.performance.now || (
object.performance.now = performance.now || performance.webkitNow ||
performance.msNow || performance.mozNow);
if (!object.performance.now){
var start = Date.now ? Date.now() : +(new Date());
if (performance.timing && performance.timing)
start = performance.timing.navigationStart
object.performance.now = (function(){
var nowOffset = Date.now ? Date.now() : +(new Date());
return nowOffset - start;
});
}
object.performance.mark || (
object.performance.mark =
object.performance.webkitMark ? object.performance.webkitMark :
(function (label) {
marks.push({'name':label,'entryType':'mark','startTime':object.performance.now(),'duration':0});
}));
object.performance.getEntriesByType || (
object.performance.getEntriesByType =
object.performance.webkitGetEntriesByType ? object.performance.webkitGetEntriesByType :
(function (type) {
return type == 'mark' ? marks : undefined;
}));
}());
markUserTime = function(label) {
window.performance.mark(label);
if (console && console.timeStamp)
console.timeStamp(label);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment