Created
December 17, 2014 14:47
-
-
Save gabrysiak/8cb72acaff9ca399ca83 to your computer and use it in GitHub Desktop.
Javascript get function execution time
This file contains hidden or 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
// 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