Tracking JavaScript Statistics
It can be very useful to track custom statistics on JavaScript functions. For example, tracking all the times console.log is called:
console.log = track(console.log); console.log('message1'); console.log('message2'); console.log('message3'); console.log(console.log.called);// 3
Since this is JavaScript, the implementation is very simple:
var track = function(tracking) { return function tracker() { // Must transform the arguments sent in into an array which // .apply sends as arguments to the function var args = Array.prototype.slice.call(arguments); tracking.apply(this, args); // tracker.called is only falsy, when 0 or undefined here tracker.called = (tracker.called || 0) + 1; } }
Of course most JS profilers (such as browser based ones) track function call counts, but the above can be modified to track any statistic.
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2014/trackingJSStatistics.js


















