// Chrome
// https://developer.chrome.com/devtools/docs/console-api
// Safari
// https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html
// Firefox
// https://developer.mozilla.org/ja/docs/Web/API/console
// IE
// https://msdn.microsoft.com/ja-jp/library/hh772183(v=vs.85).aspx
Last active
August 21, 2017 02:45
-
-
Save Leko/844cbe0041f5c6982b76 to your computer and use it in GitHub Desktop.
Any function profiling
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
function profilify(fn, label) { | |
var displayName = label || fn.name, | |
called = 0, | |
profilified = function() { | |
var args = [].slice.call(arguments), | |
startedAt = new Date(), | |
defaults = { total: 0, times: 0, average: 0 }, | |
ret; | |
profilify._stats[displayName] = profilify._stats[displayName] || defaults | |
console.group(displayName + '(' + JSON.stringify(args).slice(1, -1) + ') ' + (++called) + ' times'); | |
console.log('called with:', args); | |
console.timeStamp(displayName + 'call'); | |
console.time(displayName); | |
ret = fn.apply(this, arguments); | |
var spent = new Date() - startedAt; | |
profilify._stats[displayName].times += 1; | |
profilify._stats[displayName].total += spent; | |
profilify._stats[displayName].average = profilify._stats[displayName].total / profilify._stats[displayName].times; | |
console.timeEnd(displayName); | |
console.timeStamp(displayName + 'called'); | |
console.log('return value:', ret); | |
console.groupEnd(displayName); | |
return ret; | |
}; | |
return profilified; | |
} | |
profilify._stats = {} | |
profilify.stats = function() { | |
var totalAll = 0, | |
results = []; | |
for (var displayName in profilify._stats) { | |
totalAll += profilify._stats[displayName].total; | |
results.push({ | |
displayName: displayName, | |
total: profilify._stats[displayName].total, | |
times: profilify._stats[displayName].times, | |
average: profilify._stats[displayName].average, | |
}) | |
} | |
results.sort(function(a, b) { return a.total < b.total }) | |
console.log('total:', totalAll) | |
for (var i in results) { | |
console.log(results[i].displayName, ',', results[i].total, 's,', (results[i].total / totalAll * 100), '%,', results[i].times, 'times') | |
} | |
}; |
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
// --- Demo --- | |
var obj = { | |
hoge: function() { | |
var i = 10000; | |
while(i--) { | |
new Array(i); | |
} | |
return this.foo(i); | |
}, | |
foo: function foo() { | |
return 1; | |
} | |
}; | |
obj.hoge = profilify(obj.hoge, 'hoge function'); | |
obj.foo = profilify(obj.foo); | |
obj.hoge(); | |
profilify.stats(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment