Skip to content

Instantly share code, notes, and snippets.

@prestonp
Last active August 29, 2015 14:01
Show Gist options
  • Save prestonp/ffad59168ba19e687865 to your computer and use it in GitHub Desktop.
Save prestonp/ffad59168ba19e687865 to your computer and use it in GitHub Desktop.
stampy

Usage

Stampy is used to log time differences. You could use it to profile time-consuming operations.

Note that the first .log() begins the starting timestamp. Consecutive .log() just prints the diff from the last timestamp.

var stampy = require('stampy');
var userStampy = new stampy({ mute: process.env === 'production' || config.g.enableTimestamps });

userStampy.log('Recording timestamps now');

db.user.findById(45, function(err, user) {
  userStampy.log('User lookup');
});

prints the following

Recording timestamps now
User lookup - 127ms
// Simple app timestamping
var stampy = function(opts) {
opts = opts || {};
this.mute = opts.mute;
};
stampy.prototype.log = function(description) {
if (this.mute ) return;
if (!this.timestamp) {
this.timestamp = new Date();
console.log(description);
return;
}
var now = new Date();
var diff = now - this.timestamp;
this.timestamp = now;
console.log(description + ' - ' + diff + 'ms');
}
stampy.prototype.reset = function() {
this.timestamp = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment