profiling.ts 1.4 KB

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