Last active
August 29, 2015 14:11
-
-
Save harry1989/0bffd1e8451fe9e1d601 to your computer and use it in GitHub Desktop.
HTML5 Instrumented code with Proxies and the navigation timing API
This file contains 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 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