Skip to content

Instantly share code, notes, and snippets.

@gabrysiak
Created December 17, 2014 14:47
Show Gist options
  • Save gabrysiak/8cb72acaff9ca399ca83 to your computer and use it in GitHub Desktop.
Save gabrysiak/8cb72acaff9ca399ca83 to your computer and use it in GitHub Desktop.
Javascript get function execution time
// Taken from
// http://calendar.perfplanet.com/2014/performance-measurements-using-chrome-devtools-code-snippets/
var primesApp = {
findFirstPrimes: function () {
var n = Number(document.querySelector('#n').value);
console.log('finding first', n, 'primes');
var primes = findFirstPrimes(n);
renderPrimes(primes);
}
};
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('#find').addEventListener('click', function () {
primesApp.findFirstPrimes();
});
});
(function timeMethodCall() {
var object = primesApp;
var methodName = 'findFirstPrimes';
var originalMethod = object[methodName];
console.assert(typeof originalMethod === 'function', 'cannot find method ' + methodName);
object[methodName] = function () {
console.time(methodName);
originalMethod.call(object);
console.timeEnd(methodName);
// restore original methodName
object[methodName] = originalMethod;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment