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.

Revisions

  1. pirey created this gist Dec 28, 2023.
    23 changes: 23 additions & 0 deletions withMeasure.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@

    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
    }