profiling.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ComponentInternalInstance, formatComponentName } from './component'
  2. let supported: boolean
  3. let perf: any
  4. export function startMeasure(
  5. instance: ComponentInternalInstance,
  6. type: string
  7. ) {
  8. if (instance.appContext.config.performance && isSupported()) {
  9. perf.mark(`vue-${type}-${instance.uid}`)
  10. }
  11. }
  12. export function endMeasure(instance: ComponentInternalInstance, type: string) {
  13. if (instance.appContext.config.performance && isSupported()) {
  14. const startTag = `vue-${type}-${instance.uid}`
  15. const endTag = startTag + `:end`
  16. perf.mark(endTag)
  17. perf.measure(
  18. `<${formatComponentName(instance, instance.type)}> ${type}`,
  19. startTag,
  20. endTag
  21. )
  22. perf.clearMarks(startTag)
  23. perf.clearMarks(endTag)
  24. }
  25. }
  26. function isSupported() {
  27. if (supported !== undefined) {
  28. return supported
  29. }
  30. /* eslint-disable no-restricted-globals */
  31. if (typeof window !== 'undefined' && window.performance) {
  32. supported = true
  33. perf = window.performance
  34. } else {
  35. supported = false
  36. }
  37. /* eslint-enable no-restricted-globals */
  38. return supported
  39. }