get-performance.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // miniprogram/page/API/pages/get-performance/get-performance.js
  2. const util = require('./util')
  3. const performance = wx.getPerformance ? wx.getPerformance() : {}
  4. const performanceObserver = performance.createObserver ? performance.createObserver() : null
  5. Page({
  6. onShareAppMessage() {
  7. return {
  8. title: '周期性缓存',
  9. path: 'packageAPI/pages/performance/get-performance/get-performance'
  10. }
  11. },
  12. data: {
  13. theme: 'light',
  14. array: [],
  15. support: false,
  16. },
  17. onLoad() {
  18. this.setData({
  19. theme: wx.getSystemInfoSync().theme || 'light'
  20. })
  21. if (wx.onThemeChange) {
  22. wx.onThemeChange(({theme}) => {
  23. this.setData({theme})
  24. })
  25. }
  26. console.log('canIUse:getPerformance:', wx.canIUse('getPerformance'))
  27. let canIUse = false
  28. if (wx.getPerformance) {
  29. canIUse = true
  30. }
  31. this.setData({
  32. support: canIUse,
  33. })
  34. },
  35. getPerformanceInfo() {
  36. const EntryList = performance.getEntries()
  37. const array = []
  38. EntryList.forEach((item) => {
  39. array.push({
  40. entryType: util.renderEntryType(item.entryType),
  41. name: util.renderName(item.name),
  42. duration: util.renderDuration(item.duration),
  43. startTime: util.renderStartTime(item.startTime),
  44. })
  45. })
  46. this.setData({
  47. array,
  48. })
  49. },
  50. startObserver() {
  51. // 监听需要的性能指标
  52. performanceObserver.observe({entryTypes: ['render', 'script', 'navigation']})
  53. },
  54. stopObserver() {
  55. // 结束监听
  56. performanceObserver.disconnect()
  57. }
  58. })