Skip to content

Instantly share code, notes, and snippets.

@harry1989
Last active August 29, 2015 14:11
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 harry1989/0bffd1e8451fe9e1d601 to your computer and use it in GitHub Desktop.
Save harry1989/0bffd1e8451fe9e1d601 to your computer and use it in GitHub Desktop.
HTML5 Instrumented code with Proxies and the navigation timing API
function getInstrumentedFn(fn){
return new Proxy(fn, {
apply: function(target, that, args){
//get the function name
var fName = fn.name || ('random function_' + parseInt(Math.random() * 1000));
//mark the starting of function.
performance.mark(fName + '_start');
var returnValue = fn.apply(that, args);
//end of the function
performance.mark(fName + '_end');
performance.measure(fName + '_performance', fName + '_start', fName + '_end');
var duration = performance.getEntriesByName(fName + '_performance')[0]['duration'];
// Add the timing information.
console.log('The function ' + fName + ' took ' + duration + ' ms');
return returnValue;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment