profiling.ts 1.4 KB

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