Skip to content

Commit

Permalink
use performance APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Mar 2, 2025
1 parent 5f84bc4 commit cb64243
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/utils/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@ export class Timer {
/** @type {number|null} */
startTime = null

/** @type {Object.<string, number>} */
stepTimestamps = {}

start() {
this.startTime = performance.now()
this.stepTimestamps = {}
this.mark('start')
this.startTime = this.mark('start')
}

/**
* @param {string} step
* @param {string} markName
* @returns {number}
*/
mark(step) {
if (!this.startTime) return
const now = performance.now()
this.stepTimestamps[step] = now
console.info(`[Timer] ${step} at ${((now - this.startTime) / 1000).toFixed(2)}s`)
mark(markName) {
const now = performance.mark(markName).startTime
if (this.startTime) console.info(`[Timer] ${markName} at ${((now - this.startTime) / 1000).toFixed(2)}s`)
return now
}

/**
* @param {string} reason
*/
stop(reason) {
if (!this.startTime) return null
this.mark(reason)
console.debug({
reason,
totalDuration: this.stepTimestamps[reason],
steps: this.stepTimestamps,
const measure = performance.measure('flash', {
start: this.startTime,
end: this.mark(reason),
detail: reason,
})
// TODO: submit results
console.log(measure)
console.log(performance.getEntriesByType('mark'))
}
}

0 comments on commit cb64243

Please sign in to comment.