Created
December 28, 2023 10:57
-
-
Save pirey/499264af254e5efb33a0ce53438c19d5 to your computer and use it in GitHub Desktop.
print simple perf measurement
This file contains 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
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