vue3-yuanma/packages/runtime-core/src/profiling.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import { ComponentInternalInstance, formatComponentName } from './component'
let supported: boolean
let perf: any
export function startMeasure(
instance: ComponentInternalInstance,
type: string
) {
if (instance.appContext.config.performance && isSupported()) {
perf.mark(`vue-${type}-${instance.uid}`)
}
}
export function endMeasure(instance: ComponentInternalInstance, type: string) {
if (instance.appContext.config.performance && isSupported()) {
const startTag = `vue-${type}-${instance.uid}`
const endTag = startTag + `:end`
perf.mark(endTag)
2020-06-26 22:07:07 +08:00
perf.measure(
`<${formatComponentName(instance, instance.type)}> ${type}`,
startTag,
endTag
)
perf.clearMarks(startTag)
perf.clearMarks(endTag)
}
}
function isSupported() {
if (supported !== undefined) {
return supported
}
/* eslint-disable no-restricted-globals */
if (typeof window !== 'undefined' && window.performance) {
supported = true
perf = window.performance
} else {
supported = false
}
/* eslint-enable no-restricted-globals */
return supported
}