Skip to content

Instantly share code, notes, and snippets.

@pirey
Created December 28, 2023 10:57
Show Gist options
  • Save pirey/499264af254e5efb33a0ce53438c19d5 to your computer and use it in GitHub Desktop.
Save pirey/499264af254e5efb33a0ce53438c19d5 to your computer and use it in GitHub Desktop.
print simple perf measurement
async function withMeasure<T>(label: string = 'Measurement', fn: () => Promise<T>) {
function bytes2MB(bytes: number): number {
return bytes / (1024 * 1024)
}
const pstart = performance.now()
const mstart = process.memoryUsage().heapUsed
const result = await fn()
const mend = process.memoryUsage().heapUsed
const mused = mend - mstart
const pend = performance.now()
const duration = pend - pstart
console.log(`${label}: ${duration} milliseconds ${mused} bytes ${bytes2MB(mused)} MB`)
console.log(`mStart: ${mstart} mEnd: ${mend} bytes`)
console.log(`pStart: ${pstart} pEnd: ${pend} bytes`)
console.log('---')
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment