Skip to content

Instantly share code, notes, and snippets.

@zaydek
Last active January 25, 2020 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaydek/254b1462cf0f878e57d9d6f2ac5fe937 to your computer and use it in GitHub Desktop.
Save zaydek/254b1462cf0f878e57d9d6f2ac5fe937 to your computer and use it in GitHub Desktop.
BEGIN TRANSACTION;
INSERT INTO user (username, password_digest) VALUES (?, ?);
INSERT INTO session (username, session, "create", expire) VALUES (?, ?, DATETIME("now"), DATETIME("now", "+30 seconds"));
COMMIT;
import invariant from "invariant"
// A `PerfTimer` represents a performance timer, measured in
// milliseconds as per `Date.now()`.
class PerfTimer {
constructor() {
Object.assign(this, {
_state: {
t1: 0,
t2: 0,
result: -1,
},
})
}
// `reset` resets the timer.
reset() {
Object.assign(this, new this.constructor())
}
// `on` times the execution duration of a synchronous
// function.
on(timedFn) {
this.restart()
timedFn()
this.stop()
}
// `asyncOn` synchronously times the execution duration of
// an asynchronous function.
async asyncOn(asyncTimedFn) {
this.restart()
await asyncTimedFn()
this.stop()
}
// `restart` restarts the timer.
restart() {
this.reset()
this.start()
}
// `start` starts the timer.
start() {
invariant(
!this._state.t1 && !this._state.t2,
"PerfTimer: A performance timer cannot be started more than once.",
)
this._state.t1 = Date.now()
}
// `stop` stops the timer.
stop() {
invariant(
this._state.t1 && !this._state.t2,
"PerfTimer: A performance timer cannot be stopped more than once.",
)
this._state.t2 = Date.now()
this._state.result = this._state.t2 - this._state.t1
}
// `duration` returns the execution duration of the timer.
duration() {
invariant(
this._state.result !== -1,
"PerfTimer: A performance timer cannot be measured before code execution occurs. " +
"You can use `perfTimer.on(timedFn)` to run the timer.",
)
return this._state.result
}
}
export default PerfTimer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment