I use this nodejs module to measure / profile how long parts of my application take to execute.
// polyfill for window.performance.now
var performance = global.performance || {}
var performanceNow =
performance.now ||
performance.mozNow ||
performance.msNow ||
performance.oNow ||
performance.webkitNow ||
function(){ return (new Date()).getTime() }
// generate timestamp or delta
// see http://nodejs.org/api/process.html#process_process_hrtime
function hrtime(previousTimestamp){
var clocktime = performanceNow.call(performance)*1e-3
var seconds = Math.floor(clocktime)
var nanoseconds = Math.floor((clocktime%1)*1e9)
if (previousTimestamp) {
seconds = seconds - previousTimestamp[0]
nanoseconds = nanoseconds - previousTimestamp[1]
if (nanoseconds<0) {
seconds--
nanoseconds += 1e9
}
}
return [seconds,nanoseconds]
}
function clock(start) {
if ( !start ) return hrtime();
var end = hrtime(start);
return Math.round((end[0]*1000) + (end[1]/1000000));
}
module.exports = clock;
Usage is pretty straight forward:
time = benchmark();
to start counter and time = benchmark(time);
to measure duration since the previous call.
This includes a polyfill for when my application need to run on the browser.
The function seems to work well but it severely (and ironically) affects performance, especially (and unsurprisingly) in Internet Explorer.
How can I make it quicker?