Skip to content

Instantly share code, notes, and snippets.

@xivSolutions
Created January 28, 2014 03:41
Show Gist options
  • Save xivSolutions/8661922 to your computer and use it in GitHub Desktop.
Save xivSolutions/8661922 to your computer and use it in GitHub Desktop.
JavaScript Timer Function
var start = process.hrtime();
var elapsed_time = function(note){
var precision = 3; // 3 decimal places
var elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
console.log(process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + note); // print message + time
start = process.hrtime(); // reset the timer
}
// Example:
var debug = true;
http.createServer(function(request, response) {
if(debug) console.log("----------------------------------");
if(debug) elapsed_time("recieved request");
var send_html = function(err, contents) {
if(debug) elapsed_time("start send_html()");
response.writeHead(200, {'Content-Type': 'text/html' } );
response.end(contents);
if(debug) elapsed_time("end send_html()");
}
if(debug) elapsed_time("start readFile()");
fs.readFile('output.txt', send_html);
if(debug) elapsed_time("end readFile()");
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment