Skip to content

Instantly share code, notes, and snippets.

@kevinkace
Last active June 17, 2021 20:12
Show Gist options
  • Save kevinkace/e5b8ed060a6e70dfe4e4ecc6618ae160 to your computer and use it in GitHub Desktop.
Save kevinkace/e5b8ed060a6e70dfe4e4ecc6618ae160 to your computer and use it in GitHub Desktop.
see how long things take, without the fuckery of perf_hooks, so it's probably not terribly accurate, but it's easy
// usage:
// const timer = new Timer("timerName");
// doThing();
// timer.measure("didThing");
// await doThing2();
// timer.measure("didThing2");
// timer.report();
// ┌─────────┬─────────────┬───────────────┬──────────┬───────────┐
// │ (index) │ name │ time │ duration │ sincePrev │
// ├─────────┼─────────────┼───────────────┼──────────┼───────────┤
// │ 0 │ 'didThing' │ 1623960178569 │ 502 │ 502 │
// │ 1 │ 'didThing2' │ 1623960180577 │ 2510 │ 2008 │
// └─────────┴─────────────┴───────────────┴──────────┴───────────┘
module.exports = class Timer {
constructor(name) {
this.name = name;
this.epoch = Date.now();
this.measurements = [];
}
measure(name) {
const time = Date.now();
this.measurements.push({
name,
time,
duration : time - this.epoch,
sincePrev : time - (
this.measurements.length ?
this.measurements[this.measurements.length - 1].time :
this.epoch
)
});
}
report() {
console.table(this.measurements);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment