scientist
Version:
Carefully refactor critical paths in production
55 lines (41 loc) • 1.1 kB
JavaScript
var Measurement, Stopwatch;
Stopwatch = (function() {
function Stopwatch() {
this.reset();
}
Stopwatch.prototype.reset = function() {
return this._start = process.hrtime();
};
Stopwatch.prototype.time = function() {
return this._hrToMs(process.hrtime(this._start));
};
Stopwatch.prototype._hrToMs = function(arg) {
var nanoseconds, seconds;
seconds = arg[0], nanoseconds = arg[1];
return Math.round(seconds * 1e3 + nanoseconds / 1e6);
};
return Stopwatch;
})();
Measurement = (function() {
function Measurement(stopwatch) {
this._stopwatch = stopwatch;
this.elapsed = stopwatch.time();
Object.freeze(this);
}
Measurement.benchmark = function(block) {
var stopwatch;
stopwatch = new Stopwatch();
block();
return new Measurement(stopwatch);
};
Measurement.prototype.remeasure = function(block) {
block();
return new Measurement(this._stopwatch);
};
Measurement.prototype.preserve = function(block) {
block();
return this;
};
return Measurement;
})();
module.exports = Measurement;