import { isUserTimingAPISupported } from './is-user-timing-api-supported' ; import { isPerformanceObservableSupported } from './is-performance-observable-supported' ; import { isNodeJSEnv } from './is-nodejs-env' ; let marksMap = { } ; let marksObserver = { } ; returns const getTimeNow = ( ) : number => ( isUserTimingAPISupported ? performance . now ( ) : Date . now ( ) ) ; param markName returns const clear = ( markName ) : void => { marksMap [ markName ] = undefined ; if ( ! ! marksObserver [ markName ] ) { marksObserver [ markName ] = undefined ; } if ( ! isUserTimingAPISupported ) { return ; } if ( ! isNodeJSEnv ) { performance . clearMeasures ( markName ) ; } performance . clearMarks ( markName ) ; } ; param markName returns const start = ( markName ) : void => { if ( isUserTimingAPISupported ) { if ( isNodeJSEnv && isPerformanceObservableSupported ) { const obs = new PerformanceObserver ( list => { marksObserver [ markName ] = list . getEntries ( ) . find ( f => f . name === markName ) ; obs . disconnect ( ) ; } ) ; obs . observe ( { entryTypes : [ 'measure' ] } ) ; } performance . mark ( markName ) ; } marksMap [ markName ] = getTimeNow ( ) ; } ; /**
 * Response type of `PerfMarks.end()` method
 *
 */ export type PerfMarksPerformanceEntry = PerformanceEntry | { duration ? : number ; startTime ? : number ; name ? : string ; entryType ? : string ; } ; param markName param markNameToCompare returns const end = ( markName , markNameToCompare ? ) : PerfMarksPerformanceEntry => { try { const startTime = marksMap [ markName ] ; if ( ! isUserTimingAPISupported ) { return startTime ? ( { duration : getTimeNow ( ) - startTime , startTime , entryType : 'measure' , name : markName } as PerformanceEntry ) : { } ; } if ( ! markNameToCompare ) { performance . mark ( `${ markName }-end` ) ; } performance . measure ( markName , markName , markNameToCompare || `${ markName }-end` ) ; if ( isNodeJSEnv ) { if ( ! ! marksObserver [ markName ] ) { return marksObserver [ markName ] as PerfMarksPerformanceEntry ; } return startTime ? ( { duration : getTimeNow ( ) - startTime , startTime , entryType : 'measure' , name : markName } as PerformanceEntry ) : { } ; } const entry = performance . getEntriesByName ( markName ) . pop ( ) ; return entry || { } ; } catch ( e ) { return { } ; } finally { clear ( markName ) ; clear ( markNameToCompare || `${ markName }-end` ) ; } } ; returns const clearAll = ( ) : void => { marksMap = { } ; marksObserver = { } ; if ( ! isUserTimingAPISupported ) { return ; } if ( ! isNodeJSEnv ) { performance . clearMeasures ( ) ; } performance . clearMarks ( ) ; } ; export { start , end , clear , clearAll , isUserTimingAPISupported , isPerformanceObservableSupported } ;