Last active
June 17, 2021 20:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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